For educational purpose I'm implementing a tic tac toe game in Python.
The function which is setting the players X or O is recursive:
def set_marker(player,board):
print "\nplayer",player
x = y = 3
while(x not in range(0,3)):
x = input("x: ")
while(y not in range(0,3)):
y = input("y: ")
if board[x][y] == 0:
board[x][y]=player
return board
else:
set_marker(player,board)
# return board
Parameters:
board = 2dimensional list ( [[0, 0, 0], [0, 0, 0], [0, 0, 0]] )
player = int (value = '1' or '2')
If I set my 'X' to an already used field I'm calling the function again. When this case happens and I'm using 'board' in my main loop again the script is throwing following error:
Python: TypeError: 'NoneType' object has no attribute '__getitem__'
The type of board is in this case: none.
I solved the problem by simply returning board also in the else: part.
Here goes my question:
Why do I have to return board also within else, as I'm calling the function until I return the proper board?