python中的列表索引错误,tic-tac-toe-gam

2024-06-25 23:11:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从这个页面上学习python课程。 https://inventwithpython.com/chapter10.html 我已经创建了井字塔克托键入所有代码从上述网站自己

有时代码可以正常工作。有时候我犯了这个错误,游戏就崩溃了

Traceback (most recent call last):
  File "E:\Python\tictac.py", line 139, in <module>
    makeMove(theBoard, computerLetter, move)
  File "E:\Python\tictac.py", line 40, in makeMove
    board[move] = letter
TypeError: list indices must be integers, not NoneType

这是我输入的游戏代码

^{pr2}$

Tags: 代码inpyhttpscom游戏moveline
1条回答
网友
1楼 · 发布于 2024-06-25 23:11:44

您应该转换:

def chooseRandomMoveFromList(board, moveList):
    possibleMoves = []
    for i in moveList:
        if isSpaceFree(board, i):
            possibleMoves.append(i)

        if len(possibleMoves) != 0:
            return random.choice(possibleMoves)
        else:
            return None

^{pr2}$

问题是,由于缩进,你不能计算所有的自由位置。相反,你只检查第一个候选人的职位,如果它是免费的,当它不是你什么都不返回。在

相关问题 更多 >