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

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I realized the answer = input when I did an elif. Thanks for the help!
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.