2016-11-08 57 views
0

所以我正在通过一本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) 
+4

什么'legal'?这是不可迭代的,不是“移动”。显然,“合法”是无,但它应该是什么? –

+1

'legal_moves'返回什么? –

+0

如果您需要其他帮助,请发布'legal_moves'的定义 –

回答

1

您需要返回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) 
    return moves 
-2

功能你的问题是你不能做没有变量while循环试图算什么,所以这是问题...

,如果你给你的代码生病帮助下FIDLE你

+0

您是什么意思小提琴?喜欢在我的整个代码中弹出? – Lucky38i

+0

这是误导 - 您无法对'None'进行成员测试(即使用'in')。 'None'是有效的Python –

+0

你认为它被重写为? – Lucky38i

0

你忘了返回legal_moves

优雅的解决办法是什么是使用,而不是移动列表

+0

我不确定'屈服平方'是什么,但希望我会学到它。这个答案相当有帮助,但亨利似乎更合适 – Lucky38i

+0

for循环需要一个迭代。发电机是一种简单而有效的方法。 –

+0

生成器会立即创建返回值,而不是将它们存储在列表中(如“移动”),您可以查看http://stackoverflow.com/a/1756156/1562285或https://wiki.python.org /莫恩/发电机 –

相关问题