首页 > 解决方案 > 如何创建这个循环?

问题描述

我如何创建这个循环,如果欢迎不等于“是”或“否”,它会重复问题(如果他们有帐户),但如果欢迎等于“是”或“否”,他们会创建一个帐户(对于“否”)或允许用户登录(对于“是”)?

提前致谢

下面是我使用的代码:

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.")

标签: pythonpython-3.3

解决方案


在 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:
    ...

推荐阅读