Search code examples
pythonwhile-loopgoto

Need explanation about while-loop (use for control flow instead of goto)


I have seen some similar questions getting the answer to use a while-loop instead to "go back" in if-statements but i still trying to figure out how it all works. From the examples i've seen so far, it seems like you should set a variable to False and keep looping as long as the condition of that is "True", but i don't get it really, probably understood it completely wrong. I have some examplecode below with comments where i would want to be able to "go back" in statements but i have no idea how to actually achieve that

welcome = "If you choose to accept, you will get a list of options below. Do you accept the terms?"
terms = "" # True/False according to below

username = input("Enter your username: ")

if not username:
    print("You haven't entered a username! " + username)    # Here i would want it to start over if no username is specified
else:
    print("Hello, " + username + "\n" + welcome)

choise_terms = input("(yes/no)")

if choise_terms == "no":
    terms == False
elif choise_terms == "yes":
    terms == True
else:
    print("You have to type either \"yes\", or \"no\", do you accept the terms? " + choise_terms)    # Here i would want it to start over if either "yes" or "no" is specified
    
# Continue the program if terms are accepted, else close the application

So from what i understand i should be able to put my if statement inside of a whileloop somehow, and as long as yet another variable is set to True, the loop will continue?


Solution

  • There are basically two options that come to mind. The first is to have the loop run infinitely with while True and use break to terminate as soon as a certain condition is met, like when a valid1 username is entered. Here's a version of your snippet with implemented while-loops and some additional minor refactoring.

    welcome = "If you choose to accept, you will get a list of options below. Do you accept the terms?"
    yes_no = "You have to type either \"yes\", or \"no\", do you accept the terms?"
    
    while True:
        username = input("Enter your username: ")
    
        if username:
            print("Hello,", username)
            print(welcome)
            break
        else:
            print("You haven't entered a username!")
        
    choices = ["yes", "no"]
    
    while True:
        choice_terms = input("(yes/no)")
    
        if choice_terms in choices:
            terms = choice_terms == "yes" # shorter check
            break
        else:
            print(yes_no)
    

    Alternatively, you can initialize the target variable with an invalid value, like username = '', and re-assign the user input to this variable in the loop so it will continue running until the user inputs something valid:

    username = '' # an invalid username
    
    while not username: # or: while username == ''
        username = input("Enter your username: ")
    

    1 What is or isn't "valid" depends on your application. In your case, a valid username would be a username that is a string with a length greater than zero. A valid choice in the second step is one of the strings "yes" or "no".