PixelCardist
PixelCardist

Reputation: 47

Create Loop in if / else statement

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

Upvotes: 2

Views: 90

Answers (1)

gelonida
gelonida

Reputation: 5630

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

Upvotes: 1

Related Questions