# Is it possible to use recursion to solve the following problem? I am using while and for loops right now

Write a function make_human_move(current_player, board), which does the following:

1. Print out the player's shape (e.g. if the player shape is 'X', the function prints "X's move").
2. Asks the human player for his/her move. The message asked is "Enter row and column [0 - 2]: ".
3. A move consists of two integer values separated by a space (e.g. '0 0'). You can assume the input will always be two integer values separated by a space.
4. The function checks if the move is valid or not.
a) A valid move is when the integer value is between 0 and 2 for both row and column values.
b) It also makes sure that a valid location on the board is empty (i.e. currently an empty space character ' ').
5. If a move is invalid, the function prints "Illegal move. Try again." and repeats from step 2.
6. Once a valid move is entered, the board state is updated.

This is how I am doing it right now.

def make_human_move(current_player, board):
"""Given a board state and the human piece ('O' or 'X')
ask the player for a location to play in. Repeat until a
valid response is given. Then make the move, i.e., update the board by setting the chosen cell to the player's piece.
"""
print(current_player + "'s move")
user_input = input("Enter row and column [0 - 2]: ")
input_list = user_input.split()
while True:
restart = False
for i in input_list:
if (int(i) > 2 or int(i) < 0) or (board[int(input_list[0])][int(input_list[1])] != " "):
print("Illegal move. Try again.")
user_input = input("Enter row and column [0 - 2]: ")
input_list = user_input.split()
restart = True
break
if restart:
continue
else:
board[int(input_list[0])][int(input_list[1])] = current_player
break
• The current question title of your question is too generic to be helpful. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. Commented Sep 23, 2020 at 15:40
• Why would you use recursion for this? Commented Sep 23, 2020 at 19:12

Here's my attempt. The function make_human_move just delegates the actual work to a recursive function. The function guard_range simply checks if the given argument (which must be an int) is in the expected range, and if not, it raise an exception. In the interesting part of the code, which is the recursive function make_human_move_rec, I used exceptions mainly because this way I'm able to deal with malformed input from the user (for instance, if the user inputs 0 z, the int(input_list[1]) will raise an exception).

def make_human_move(current_player, board):
print(current_player + "'s move")
make_human_move_rec(current_player, board)

def guard_range(x):
if x > 2 or x < 0: raise
return x

def make_human_move_rec(current_player, board):
user_input = input("Enter row and column [0 - 2]: ")
input_list = user_input.split()
try:
# There are three ways the following lines can raise an exception: the
# input is not formatted correctly, it's not a number, or the input is
# out of range.
input_row = guard_range(int(input_list[0]))
input_col = guard_range(int(input_list[1]))

# Also raise an exception if the board is occupied at the destination.
if board[input_row][input_col] != " ":
raise

# If we get here, then the move is valid. We do our thing.
board[input_row][input_col] = current_player
except:
print("Illegal move. Try again.")
make_human_move_rec(current_player, board)