python - 如何创建这个循环?
问题描述
我如何创建这个循环,如果欢迎不等于“是”或“否”,它会重复问题(如果他们有帐户),但如果欢迎等于“是”或“否”,他们会创建一个帐户(对于“否”)或允许用户登录(对于“是”)?
提前致谢
下面是我使用的代码:
welcome = input("Do you have an account? Please type either 'yes'/'no': ")
if welcome == "no":
while True:
username = input("OK. Let's create an account. Enter a username:")
password = input("Enter a password:")
password1 = input("Confirm password:")
if password == password1:
file = open(username+".txt", "w")
file.write(username+":"+password)
file.close()
welcome = "yes"
break
print("Passwords do NOT match!")
if welcome == "yes":
while True:
login1 = input("OK. Enter your username:")
login2 = input("Enter your password:")
file = open(login1+".txt", "r")
data = file.readline()
file.close()
if data == login1+":"+login2:
print("You have successfully logged in as, " + login1)
print("")
print("Welcome to the Music Quiz!")
break
print("Incorrect username or password.")
解决方案
在 ask for 周围放置一个循环welcome
。
while True:
welcome = input("Do you have an account? Please type either 'yes'/'no': ")
if welcome in ("yes", "no"):
break
if welcome == "no":
...
else:
...
推荐阅读
- reactjs - 如何访问 electron.app.getPath()?
- java - 一次处理两个工作簿 - Apache POI
- python - Raspberry Pi 3 B+ BLE 升级后扫描失败(使用 Stretch)
- python - 烧瓶 werkzeug.exceptions.BadRequestKeyError
- apache - 在此服务器上找不到请求的 URL /login。
- c# - 使用复杂的 XML 数据将 XML 转换为 JSON
- javascript - 将未定义的加上 JavaScript 中的值扔给 PHP
- keyboard-shortcuts - 到达一行代码的末尾
- php - 将旧的格式化 URL 重写为新的格式化 URL
- c++ - 如何制作一个范围以使用基于范围的范围(以取代传统的 for)?