【发布时间】: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