Lucky38i Lucky38i - 1 month ago 15
Python Question

Python Game | TypeError: argument of type 'NoneType' is not iterable

So I'm working through a python book and was asked to create a tic-tac-toe game and understand the code which I do, relatively. Come time to run the program and I was given this weird error


TypeError: argument of type 'NoneType' is not iterable


with the full error being:

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


Here is the code it refers to in
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)


The error occurs in the following function:

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


I've tried changing
move = None
to
move = ' '
but that made no difference. any ideas?

As requested here's the function for
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)

Answer

You need to return the moves list:

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