Loe*_*sen 2 python tuples typeerror
我现在已经多次询问过这个问题了.但是,答案似乎没有解决我的问题.我得到一个类型错误,'tuple'对象不可调用.即使列表中的元组以正确的方式用逗号分隔,我也会得到这个:
def aiMove(b):
movesList = moves(b, -1)
heuristic = []
if movesList:
for m in movesList:
bt = copy.deepcopy(b)
print("bt: ", bt)
bt[m[0]][m[1]] = -1
h = heat[m[0]][m[1]]
for move in m[2]:
i=1;
try:
while (-1* bt[m[0] + i*move[0]][m[1] + i*move[1]] < 0):
bt[m[0] + i*move[0]][m[1] + i*move[1]] *= -1
bt[m[0] + i*move[0]][m[1] + i*move[1]] += -1
i += 1;
except IndexError:
continue
alpha = max(float('-inf'), alphabeta(bt, depth-1, h, float('-inf'), float('inf'), 1))
heuristic.append(alpha)
if (float('inf') <= alpha):
break
selectedMove = movesList[int(heuristic.index(max(heuristic)))]
move(b, selectedMove, -1)
else:
print ("The AI can't move!")
return []
Run Code Online (Sandbox Code Playgroud)
例如,selectedMove是(3,2,[(0,1)]).moves()函数返回一个移动列表,如selectedMove的最终值.它是用于游戏树搜索的alpha beta修剪实现.move()函数实际上将棋子移动到该位置并更新棋盘以完成计算机的转弯.变量b代表当前游戏板上的值我尝试的每次转换(即强制selectedMove本身就是一个列表,等等)将在行"move(b,selectedMove,-1)"中给出相同的错误
有人看到可能出错的地方吗?
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1489, in __call__
return self.func(*args)
File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\DrawBoard.py", line 131, in eventfun
placePiece(x, y, b, n)
File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\DrawBoard.py", line 119, in placePiece
project2.aiMove(b)
File "C:\Users\Loek Janssen\Documents\GitHub\CITS1401-Project\project2.py", line 225, in aiMove
move(b, selectedMove, -1)
TypeError: 'tuple' object is not callable
Run Code Online (Sandbox Code Playgroud)
在这一行中,您说您将使用该名称move来引用以下元素m[2]:
for move in m[2]:
Run Code Online (Sandbox Code Playgroud)
但后来,你试图调用一个函数,你已经叫move:
move(b, selectedMove, -1)
Run Code Online (Sandbox Code Playgroud)
一旦你看到这个,错误信息就完全有道理了:
TypeError: 'tuple' object is not callable
Run Code Online (Sandbox Code Playgroud)
因为名称move不再引用该函数,而是引用它在循环中绑定的最后一个元组.
比修复此特定错误更重要(move如果您还想使用它来引用函数,请不要使用变量名称)正在识别解释器如何准确地告诉您问题是什么.
它说元组不可调用; 在它所抱怨的代码行中,你正在打电话move; 因此move是一个元组,你的下一步应该是添加print(move)(或者print(type(move), repr(move))如果你想成为幻想),看看它实际上是什么元组.