Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会得到';str';对象不可调用";将变量追加到列表时?_Python_List_Variables_Append_Callable - Fatal编程技术网

Python 为什么我会得到';str';对象不可调用";将变量追加到列表时?

Python 为什么我会得到';str';对象不可调用";将变量追加到列表时?,python,list,variables,append,callable,Python,List,Variables,Append,Callable,我创建了一个函数,它创建了一个空列表,并将另一个列表中的值分配给一个变量。然后,每次调用函数时,代码都应该将变量的值附加到列表中。函数被放置在while循环中,因此它应该继续追加。但是,在添加第一个元素之后,我得到一个错误,告诉我string对象不可调用。代码如下。我需要这个做模拟考试。有什么想法吗 def tracker(): global tracker global trackerresult trackerresult = [] tracker = opp

我创建了一个函数,它创建了一个空列表,并将另一个列表中的值分配给一个变量。然后,每次调用函数时,代码都应该将变量的值附加到列表中。函数被放置在while循环中,因此它应该继续追加。但是,在添加第一个元素之后,我得到一个错误,告诉我string对象不可调用。代码如下。我需要这个做模拟考试。有什么想法吗

def tracker():
    global tracker
    global trackerresult
    trackerresult = []
    tracker = opposite1[decider]
    trackerresult.append(tracker)
    print(trackerresult)
结果:

Traceback (most recent call last):
  File "C:\Users\Timic\Downloads\Python Opposites Prorgam (1).py", line 47, in <module>
tracker()
TypeError: 'str' object is not callable   
回溯(最近一次呼叫最后一次):
文件“C:\Users\Timic\Downloads\Python-opposities-Prorgam(1.py)”,第47行,在
跟踪器()
TypeError:“str”对象不可调用

跟踪器
全局变量和
跟踪器
函数之间存在名称冲突。运行
def tracker():…
时,它将变量
tracker
设置为函数对象。然后,当您第一次调用
tracker
函数时,它会修改全局变量
tracker
,以显式地指向
opposite1[decider]
的结果,这是一个字符串。下一次调用
跟踪器
函数的尝试失败,因为该函数已被字符串覆盖

请用语言(我想是python)添加一个标记。为此,我通过将函数名更改为track来修复这个错误。但是现在出现了一个新的逻辑错误,它没有将变量
tracker
附加到列表
trackerresult
中。它只是取代了它。为什么?@TimicNdoloka我不明白你的问题是什么,但它与这个全局变量跟踪问题无关,应该对此表示感谢,我将发布一个新问题。