【发布时间】:2018-01-12 09:05:51
【问题描述】:
我有一个关于我的方法错误的小问题(如在一个类中),我目前正在研究一个想要找出机器人可以做的最佳移动的 AI,但是当我想返回 bestMove 时,它告诉我的错误。
def computerMove(self, tile, newBoard, legalMoves, isOnCorner):
legitMoves = self.getLegalMoves(self.board, tile)
for x, y in legitMoves:
if self.isOnCorner(x, y):
return [x, y]
highestPoints = -1
for x, y in legitMoves:
computerBoard = self.getComputerBoard(self.newBoard)
makeYourMove(computerBoard, tile, x, y)
points = countPoints(computerBoard)[tile]
if points > highestPoints:
highestPoints = points
bestMove = [x][y]
return bestMove
但错误状态
UnboundLocalError:赋值前引用了局部变量“bestMove”
【问题讨论】:
-
你必须在for循环之前给
bestMove赋值,因为如果if条件为假,bestMove没有赋值,函数会返回None。跨度> -
检查我的更新! @ikreb
标签: python python-3.x