需要有关 while-loop 的解释(用于控制流程而不是 goto)

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

我看到一些类似的问题得到了使用 while 循环而不是在 if 语句中“返回”的答案,但我仍在努力弄清楚它是如何工作的。从我到目前为止看到的示例来看,似乎您应该将变量设置为 False 并在条件为“真”时一直循环,但我真的不明白,可能是理解了这是完全错误的。我在下面有一些带有注释的示例代码,我希望能够在语句中“返回”,但我不知道如何真正实现它

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

所以据我所知,我应该能够以某种方式将我的 if 语句放在 whileloop 中,只要将另一个变量设置为 True,循环就会继续?

基本上有两种选择。 第一种是使用 while True 无限循环 运行 并使用 break 在满足特定条件后立即终止,例如当 valid1 输入用户名。这是您的代码片段的一个版本,其中实施了 while 循环和一些额外的小重构。

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)

或者,您可以使用无效值初始化目标变量,例如 username = '',然后在循环中将用户输入重新分配给此变量,这样它将继续 运行ning 直到用户输入有效的内容:

username = '' # an invalid username

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

1 什么是“有效”或什么不是“有效”取决于您的应用程序。在您的情况下,有效的用户名将是一个长度大于零的字符串的用户名。第二步中的有效选择是字符串“yes”或“no”之一。

我写了这段代码应该可以解决你的问题(或者至少我希望它能解决):

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

如您所见,我将错误消息封装在一个 while 循环中,这样程序就不会 运行 当用户未输入有效 password.Python 时,每次用户都会检查答案输入密码,只有当它最终拥有有效的用户名时,它才会执行程序。 希望我能帮到你,问我是否需要更多信息。