0

The expected output is that it will recur until the user gives a yes or no answer, and if they give yes execute code, in this case just print "yep". everything works if the user inputs "Y" right away, but when the input is illegal and the program asks for another input the user input check stops working.

Example:

(Y/N) y
yep

ends


(Y/N) illegal

Something went wrong, make sure you made the right input and try again!

(Y/N) y

ends

What am I doing wrong ?

def user_choice():
    answer = input("(Y/N) ")
    if answer.lower() == "n": return False
    elif answer.lower() == "y": return True
    else:
        print("Something went wrong, make sure you made the right input and try again!")
        user_choice()

if user_choice(): print("yep")
Red
  • 26,798
  • 7
  • 36
  • 58
  • Is there a reason you're trying to do this with recursion? See [Asking the user for input until they give a valid result](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – G. Anderson Apr 30 '21 at 18:45
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – quamrana Apr 30 '21 at 18:50

3 Answers3

1

Don't do this recursively. That's simply the wrong choice. The solution to your immediate problem is that you need return user_choice() not just user_choice(), but an iterative solution is much smarter:

def user_choice():
    while True:
        answer = input("(Y/N) ")
        if answer.lower() == "n":
            return False
        elif answer.lower() == "y":
            return True
        else:
            print("Something went wrong, make sure you made the right input and try again!")

if user_choice():
    print("yep")
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
1

Couple things:

  1. You're missing the a line with the function signature in your code. I'll assume it's supposed to look like this:
def user_choice():
  1. You're not returning anything when the code takes the else path, so nothing gets returned when the initial choice is illegal (actually None gets returned). So update your function like so:
    answer = input("(Y/N) ")
    if answer.lower() == "n": return False
    elif answer.lower() == "y": return True
    else:
        print("Something went wrong, make sure you made the right input and try again!")
        return user_choice()  # <-- need to return a meaningful value here
  1. Recursion is a terrible choice for solving this type of problem. A loop is simpler to write, easier to read, and performs better:
def user_choice():
    while True:
        answer = input("(Y/N) ")
        if answer.lower() == "n": return False
        elif answer.lower() == "y": return True
        print("Something went wrong, make sure you made the right input and try again!")
Woodford
  • 3,746
  • 1
  • 15
  • 29
  • Thank you for the answer, but can I ask how I know if my code is optimized or not ? You mentioned that a while loop performs better, but how do I reach that conclusion on my own ? – Roee Argaman Apr 30 '21 at 21:32
  • That's a big question. Fortunately it has already been asked: https://cs.stackexchange.com/questions/56867/why-are-loops-faster-than-recursion – Woodford Apr 30 '21 at 21:39
0

You did not return the result of the recursion. You can use something like this.

def user_choice():
    answer = input("(Y/N) ")
    if answer.lower() == "n": return False
    elif answer.lower() == "y": return True
    else:
        print("Something went wrong, make sure you made the right input and try again!")
        user_choice()

if user_choice(): print("yep")
Jiří Cihelka
  • 241
  • 1
  • 4