You are calling the function recursively but don't return the recursive result. Add return
statements:
def get_counter(current_player):
counter = int(input("Select which counter you want to move."))
if counter != 1 and counter != 2 and current_player == 1:
print("This is not your counter")
print("Your counters are 1 or 2")
return get_counter(current_player)
elif counter != 3 and counter != 4 and current_player == 2:
print("This is not your counter")
print("Your counters are 3 or 4")
return get_counter(current_player)
return counter
Without explicitly returning the result of the recursive get_counter()
call, the calling function continues where it had left off before the call, and executes the return counter
statement instead. The local counter
variable is not shared between recursive function calls, so it's the first choice the player made that is eventually returned from the outermost call.
However, don't underestimate the ability of your users to continue to try and cheat; you'll eventually run into the maximum recursion depth. You shouldn't really use recursion to handle user input; use a loop instead:
def get_counter(current_player):
while True:
counter = int(input("Select which counter you want to move."))
if current_player == 1 and counter not in (1, 2):
print("This is not your counter")
print("Your counters are 1 or 2")
continue
if current_player == 2 and counter not in (3, 4):
print("This is not your counter")
print("Your counters are 3 or 4")
continue
return counter
This returns only if a correct count has been entered; it continues with the loop (reasking the question) if incorrect entries have been made instead.