1

When I run the following code, if I input the correct value of 1 or 2 on the first try, it works fine. However, if I input an incorrect value, the code then correctly prompts me to fix it. And when I then put in a correct value, instead of returning 1 or returning 2, it is returning None. Any reason why? Thanks!

def check_input(input_message, option1, option2):
    response = input(input_message + ":\n").lower()
    if int(response) == option1:
        return 1
    elif int(response) == option2:
        return 2
    else:
        print_pause("I'm sorry I don't recognize that response. Try again!", 2)
        check_input(input_message, option1, option2)

decision = check_input("Please type 1 to go into the dark cave. 2 to go into the dweilling", 1, 2)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    `return check_input(input_message, option1, option2)` – Dani Mesejo Oct 09 '21 at 15:04
  • 3
    Check the link to the duplicate. It would be easier for your function to contain a `while` loop to repeat the question rather than have a recursive call. – quamrana Oct 09 '21 at 15:05
  • 1
    Not just easier, but more efficient and more correct (as it eliminates the possibility of a `RecursionError` due to a stubborn user). – chepner Oct 09 '21 at 15:07

1 Answers1

0

Change the last line of the else block to be:

return check_input(input_message, option1, option2)

Otherwise, your function won't return any value (that's why you got None)

Gabio
  • 9,126
  • 3
  • 12
  • 32