2016-11-08 166 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 /莫恩/發電機 –

相關問題