vendredi 30 avril 2021

Trying to make a recursive function to get a y/n answer from the user, but when it recurs the check stops working

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")

Aucun commentaire:

Enregistrer un commentaire