我发现TypeError:“NoneType”对象在TictoE中不可下标,但我尝试在线解决类似问题,但没有成功

2024-10-03 02:47:56 发布

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

在结果法中,我们给出了输入板,它是由3个元素组成的列表,如TictoE和action,在一个我们想保持X或O为元组形式的位置,例如,如果我们把X放在中间,板是完全空的,那么它看起来像

 result([[None, None, None], [None, None, None], [None, None, None]], (1,1))

所以最初在第一步中,当我们第一次移动时,它并没有显示任何错误,但在第二次移动时,它显示错误

方法代码:

def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
#play = player(board)
tempboard = board
print("board...........", board)
print("Action[0] ===> ", action[0])
print("Action[1] ===> ", action[1])
if board[action[0]][action[1]] == EMPTY:
    tempboard[action[0]][action[1]] = player(board)
    print(tempboard)
    return tempboard
else:
    raise NotImplementedError('Invalid Action')

错误是:-

board........... [[None, None, None], [None, None, None], [None, None, None]]
Action[0] ===>  1
Action[1] ===>  2
[[None, None, None], [None, None, 'X'], [None, None, None]]
board........... [[None, None, None], [None, None, 'X'], [None, None, None]]
Traceback (most recent call last):
  File "C:/Users/hkfghdk/Google Drive/VIT/Harvard/CS50 AI/week0/tictactoe/runner.py", line 116, in <module>
    board = ttt.result(board, move)
  File "C:\Users\hkfghdk\Google Drive\VIT\Harvard\CS50 AI\week0\tictactoe\tictactoe.py", line 68, in result
    print("Action[0] ===> ", action[0])
TypeError: 'NoneType' object is not subscriptable

Tags: theboardnonemove错误actionresultusers