所以我正在通過一本python書,並被要求創建一個tic-tac-toe遊戲,並且相對地理解我所做的代碼。來吧時間來運行程序和我得到這個奇怪的錯誤Python遊戲| TypeError:'NoneType'類型的參數是不可迭代的
TypeError: argument of type 'NoneType' is not iterable
與完整的錯誤之中:
Traceback (most recent call last):
File "Tac Tac Toe Game revised.py", line 182, in <module>
main()
File "Tac Tac Toe Game revised.py", line 173, in main
move = human_move(board, human)
File "Tac Tac Toe Game revised.py", line 100, in human_move
while move not in legal:
TypeError: argument of type 'NoneType' is not iterable
這裏是指line 173
def main():
display_instruction()
computer,human = pieces()
turn = X
board = new_board()
display_board(board)
while not winner(board):
if turn == human:
move = human_move(board, human)
board[move] == human
else:
move = computer_move(board,computer,human)
board[move] == computer
display_board(board)
congrats_winner(the_winner,computer, human)
的代碼在以下功能中發生錯誤:
def human_move(board,human):
'''Get human move'''
legal = legal_moves(board)
move = None
while move not in legal:
move = ask_number('Where will you move? (0-8): ',0, NUM_SQUARES)
if move not in legal:
print ('\nThat square is already occupied, foolish human. Choose another.\n')
print('Fine...')
return move
我試過將move = None
更改爲move = ' '
,但這沒有什麼區別。有任何想法嗎?
如這裏要求是爲legal_moves
def legal_moves(board):
'''Creates a legal list of moves'''
moves = []
for square in range(NUM_SQUARES):
if board[square] == EMPTY:
moves.append(square)
什麼'legal'?這是不可迭代的,不是「移動」。顯然,「合法」是無,但它應該是什麼? –
'legal_moves'返回什麼? –
如果您需要其他幫助,請發佈'legal_moves'的定義 –