Python 如何创建此循环?
如果welcome不等于“是”或“否”,它会重复问题(如果他们有帐户),但如果welcome等于“是”或“否”,它们会创建一个帐户(表示“否”)或允许用户登录(表示“是”) 提前谢谢 下面是我使用的代码:Python 如何创建此循环?,python,python-3.3,Python,Python 3.3,如果welcome不等于“是”或“否”,它会重复问题(如果他们有帐户),但如果welcome等于“是”或“否”,它们会创建一个帐户(表示“否”)或允许用户登录(表示“是”) 提前谢谢 下面是我使用的代码: welcome = input("Do you have an account? Please type either 'yes'/'no': ") if welcome == "no": while True: username = input("O
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.")
可以添加跟踪用户输入状态(有效/无效)的bool变量
循环询问
欢迎
while True:
welcome = input("Do you have an account? Please type either 'yes'/'no': ")
if welcome in ("yes", "no"):
break
if welcome == "no":
...
else:
...
您只需在整件事情中添加
,而True:
,这样,当输入“错误”输入时,它将通过if
子句并返回到循环的开头,再次提示用户
while True:
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.")
非常感谢这很有帮助谢谢这很有帮助
while True:
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.")