Firstly, there's a potential recursion issue with the function. In the elif statement, the function is called again; I suppose that's to initiate the sequence again. If the user keeps entering invalid options, that will eventually lead to an overflow. Instead, try wrapping the entire function body in a while loop. You could indent the if statement to move it under the existing loop.
Also, you don't have to write "while user_int == false". You could do "while not user_int" instead.
It's returning the original value because your return statement is in the same local scope as the original answer. So, the function call in the elif statement of the original code is returning a valid answer. Then, the original function call is returning its answer which was invalid.
Here's how I rewrote it:
Also, you don't have to write "while user_int == false". You could do "while not user_int" instead.
It's returning the original value because your return statement is in the same local scope as the original answer. So, the function call in the elif statement of the original code is returning a valid answer. Then, the original function call is returning its answer which was invalid.
Here's how I rewrote it:
def user_input(number_of_answers): while True: try: answer = int(input("> ")) except: print("That's not a number!") continue if 0 < answer < number_of_answers: return answer print("That answer is not valid!")