beginner here, im interested in solving the problem but also learning the 'how' and 'why' of it
so i have this function:
import random
move = 0
def computer_move():
global move
if move == 0:
while True:
rand = random.randrange(0, 9)
if positions[rand].state:
move = 1
positions[rand].state = False
positions[rand].played = 'circle'
return positions[rand].x,positions[rand].y
before i had the 'move' variable declared outside the function everything worked fine except whenever the function is called the variable reset to 0, so i had to declare it outside and naturally i had to use it with global
.
except now when i call the function like so:
x, y = computer_move()
i get TypeError: cannot unpack non-iterable NoneType object
on the line of the function call
i tired a few ideas but nothing seems to work. Appreciate any help or clarification
In Python, a function returns None
if no return
is executed. Your function may return None
like this if move != 0
. In such a case, x, y = computer_move()
will be equivalent to x, y = None
, which is causing the TypeError
.