2

I am trying to loop this function in the case the 'else' is reached but I'm having difficulties.

I tried while False and it doesn't do the print statements, I guess it kicks out of the function as soon as it ends up being false. I tried the True and I think that's the way to go but when it hits Else it just repeats. I'm thinking... maybe I need to do another Elif for the repeat of the whole function and the else as just an escape if needed.

def login(answer):
    while False:
        if answer.lower() == "a":
            print("You selected to Login")
            print("What is your username? ")
            break
        elif answer.lower() == "b":
            print("You selected to create an account")
            print("Let's create an account.")
            break
        else:
            print("I don't understand your selection")
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Possible duplicate of [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) – wjandrea Nov 19 '19 at 01:23

1 Answers1

1
while False:

should be

while True:

otherwise you never enter the loop

Further:

else:
    print("I don't understand your selection")

should be:

else:
    print("I don't understand your selection")
    answer = input("enter a new choice")

You might even refactor your code to call the function without parameter:

def login():
    while True:
        answer = input("enter a  choice (a for login or b for account creation")
        if answer.lower() == "a":
            print("You selected to Login")
            print("What is your username? ")
            break
        elif answer.lower() == "b":
            print("You selected to create an account")
            print("Let's create an account.")
            break
        else:
            print("I don't understand your selection")
gelonida
  • 5,327
  • 2
  • 23
  • 41
  • Yeah I realized the answer = input when I did an elif. Thanks for the help! – PixelCardist Nov 19 '19 at 01:22
  • see enhanced answer. In the else case you have to ask for new input. if not you get stuck forever. I also suggested an alternative function without the answer parameter – gelonida Nov 19 '19 at 01:23