【发布时间】:2016-11-08 11:30:56
【问题描述】:
所以我正在阅读一本 Python 书籍,并被要求创建一个井字游戏并相对理解我所做的代码。到时候运行程序,我得到了这个奇怪的错误
TypeError: 'NoneType' 类型的参数不可迭代
完整的错误是:
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?那是不可迭代的,不是move。显然,legal是 None 但它应该是什么? -
legal_moves返回什么? -
如果您需要额外帮助,请发布
legal_moves的定义
标签: python python-3.x