Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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中的列表索引错误,tic tac toe游戏_Python_Python 3.x - Fatal编程技术网

python中的列表索引错误,tic tac toe游戏

python中的列表索引错误,tic tac toe游戏,python,python-3.x,Python,Python 3.x,我正在做本页的python课程。 我已经创建了tic-tac-toe,我自己输入了上面站点的所有代码 有时代码工作正常。有时我会犯这个错误,游戏就会崩溃 Traceback (most recent call last): File "E:\Python\tictac.py", line 139, in <module> makeMove(theBoard, computerLetter, move) File "E:\Python\tictac.py", line

我正在做本页的python课程。 我已经创建了tic-tac-toe,我自己输入了上面站点的所有代码

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

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
您应该转换:

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

问题是,由于压痕,您无法计算所有自由位置。相反,如果第一个候选职位是免费的,则只检查它,如果不是免费的,则不返回任何内容

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
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