0

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)
Lucky38i
  • 125
  • 1
  • 16

3 Answers3

1

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
Henry Heath
  • 1,072
  • 11
  • 19
0

You forgot to return anything in legal_moves

An elegant solution would be to use 'yield square' instead of the moves list

Maarten Fabré
  • 6,938
  • 1
  • 17
  • 36
  • I'm not sure what `yield square` is but hopefully I'll learn it. This answer was quite helpful but Henry's seemed more appropiate – Lucky38i Nov 08 '16 at 12:05
  • The for-loop needs an iterable. A generator is an easy and efficient way to make one. – Maarten Fabré Nov 08 '16 at 12:22
  • A generator creates the return values on the fly instead of storing them in a list (like your 'moves') you can check http://stackoverflow.com/a/1756156/1562285 or https://wiki.python.org/moin/Generators – Maarten Fabré Nov 08 '16 at 12:28
-2

your problem is you cant do a while loop on none variable you try to count nothing so this is the problem...

if you give a fidle of your code ill help you

InitialCrow
  • 323
  • 1
  • 5