【问题标题】:Need explanation about while-loop (use for control flow instead of goto) [duplicate]需要关于while循环的解释(用于控制流而不是goto)[重复]
【发布时间】:2021-10-27 15:02:24
【问题描述】:

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

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,您究竟需要解释什么?您是否查看了 Python 文档以了解其工作原理?
  • 是的,我也阅读了文档和其他几个示例,我想我觉得令人困惑的是示例声明您应该将变量设置为 False,然后只要条件满足就继续循环真的..(等一下..我只是把它放在假的?)我可能错过了一些非常基本的东西。将查看链接的 Try 和 except 示例,看看我是否可以完成某事:) 感谢您的回答
  • 你可能想做a basic Python tutorial。如果您对 一些 示例感到困惑,我们不可能在不知道 哪个 示例以及关于它的什么 的情况下消除混淆。

标签: python while-loop goto


【解决方案1】:

基本上有两种选择。 第一个是使用while True 无限运行循环,并在满足特定条件时使用break 终止,例如输入有效的1 用户名。这是您的 sn-p 版本,其中实现了 while-loops 和一些额外的小重构。

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 = '',并在循环中将用户输入重新分配给该变量,以便它继续运行,直到用户输入有效内容:

username = '' # an invalid username

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

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

【讨论】:

  • 您可能想澄清什么是“无效值”。
【解决方案2】:

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

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 循环中,以便在用户未输入有效密码时程序无法运行。Python 将在用户每次输入密码时检查答案并执行只有当它最终有一个有效的用户名时才可以程序。 希望对我有所帮助,如果您需要更多信息,请询问我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2011-07-27
    • 2015-04-21
    相关资源
    最近更新 更多