【问题标题】:Error: 'list' object is not callable错误:“列表”对象不可调用
【发布时间】:2017-12-06 20:10:36
【问题描述】:

我正在尝试制作一个从文件中随机提取名称的程序。将询问用户他是否想再次拿起另一个(按 1)。 名字不能重复两次。 一旦被选中,这些名字将被储存在一个列表中,写入一个文件。 当所有的名字都被选中后,程序就可以从头开始重新启动。 查了其他类似的问题,还是不明白……

from random import *

#import a list of name from a txt file
def getL1():
    l1 = open("Employees.txt", "r")
    list1 = []
    x = 0
    for line in l1:
        list1.append(line)
        x = x+1
    list1 = [el.replace('\n', '') for el in list1]

    #print("list" 1 :",list)

    return list1
#import an empty list (that will be filled by tested employees) during
#execution of the program
def getL2():
    l2 = open("tested.txt", "r")
    list2 = []

    for line in l2:
        list2.append(line)
    list2 = [el.replace('\n', '') for el in list2]

    #print("list 2 :",list2)
    l2.close()
    return list2


def listCompare():
    employees = getL1()#acquire first list from employee file
    tested = getL2()#acquire second list from tested file
    notTested = []# declare list to hole the results of the compare
    for val in employees:
        if val not in tested: #find employee values not present in tested
            #print(val) 
            notTested.append(val)#append value to the notTested list
    return notTested

def listCount():
    x=0
    employees = getL1()
    tested = getL2()
    for val in employees:
        if val not in tested:
            x = x+1

    return x
#add the names of tested employees the the second second file
def addTested(x):
    appendFile = open("tested.txt", "a")
    appenFile.write(x)
    appendFile.write('\n')
    appendFile.close()



def main():

    entry = 1
    while entry == 1:

        pickFrom = listCompare()
        if listCount() > 0:
            y = randint (0, (listCount ()-1))
            print ('\n' + "Random Employee to be tested is: ", pickFrom(y), "\n")
            addTested(pickFrom[y])
            try:
                entry = int(input("Would you like to test another employee? Enter 1:"))

            except:
                print("The entry must be a number")
                entry = 0
        else:
            print("\n/\ new cycle has begun")
            wipeFile = open("tested.txt", "w")

    print ("goodbye")
main()

我遇到的最后一个错误是:

Traceback (most recent call last):
  File "prog.py", line 78, in <module>
    main()
  File "prog.py", line 65, in main
    print ('\n' + "Random Employee to be tested is: ", pickFrom(y), "\n")
TypeError: 'list' object is not callable

【问题讨论】:

  • 列表对象需要使用[ ]调用
  • 谢谢你,我觉得自己很愚蠢...因为那个愚蠢的事情,我被困了这么久!
  • 发生在我们最好的人身上!祝你好运 !如果解决了问题,则接受发布的答案:)
  • 顺便说一句,可以对这个程序进行许多改进。为什么你有两个文件读取功能? getL1 中的 x 是什么?为什么要手动删除换行符而不是使用.splitlines?为什么你一遍又一遍地阅读相同的文件?为什么你main 循环调用listCount() 两次?为什么用appenFile.write(x); appendFile.write('\n') 而不是appenFile.write(x + '\n')?等等。

标签: python-3.x list compare


【解决方案1】:

根据代码 print pickFrom 是一个列表,当您在 print 中引用它时,需要使用 [ ] 调用它。改成pickFrom[y]

【讨论】:

  • 请不要写错字问题的完整答案,只需在评论中回复即可。错字问题不太可能对未来的读者有所帮助,因为即使您遇到完全相同的问题,也几乎不可能通过搜索找到它们。因此,系统会在几天后尝试删除此类问题,但投票/接受的答案会干扰该过程。
猜你喜欢
  • 2017-11-15
  • 1970-01-01
  • 2012-12-06
  • 2021-02-16
  • 2016-09-02
  • 2017-02-19
  • 2017-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多