0

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?

  • 3
    What exactly do you need explained about ``while``? Did you check the Python documentation on how it works? – MisterMiyagi Aug 27 '21 at 17:34
  • 1
    Does this answer your question? [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) – MisterMiyagi Aug 27 '21 at 17:35
  • Yes ive read the documentation and several other examples as well, i guess what i find confusing is that the examples state that you should set a variable to False, and then keep looping as long as the condition are True.. (Wait a minute.. I just sat it to False?) I'm probably missed something really basic. Will have a look at the Try and Except-example linked too and see if i can get something done :) thanks for the answers – NinjaSnickers Aug 27 '21 at 17:45
  • 3
    You might want to do [a basic Python tutorial](https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming). If you are confused about *some* example out there, it's impossible for us to clear up the confusion without knowing *which* example and *what* about it. – MisterMiyagi Aug 27 '21 at 17:48

2 Answers2

0

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

fsimonjetz
  • 5,644
  • 3
  • 5
  • 21
0

I wrote this code that should fix your problem (or at least i hope it does):

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

while not username:
    print("You haven't entered a username! " + username)    # Here i would want it to start over if no username is specified
    username = input("Enter your username: ")
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

As you can see i encapsulated the error message in a while loop so that the program can not run while the user as not entered a valid password.Python will check the answer every time the user enter a password and it will execute the program only when it will finnally have a valid username. Hope I helped, ask me if you want more infos.

Marthattack
  • 49
  • 1
  • 7