如何创建此循环?

How do I create this loop?

如何创建此循环,如果 welcome 不等于 "yes" 或 "no",它会重复问题(如果他们有帐户),但如果 welcome 等于 "yes" 或 "no",他们创建一个帐户("no")或允许用户登录("yes")?

提前致谢

下面是我使用的代码:

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

您可以添加一个布尔变量来跟踪用户的输入状态(valid/invalid)

validAnswer = False:
while not validAnswer:
    welcome = input("Do you have an account? Please type either 'yes'/'no': ")        
    if welcome == "no":
        validAnswer = True
        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!")

    elif welcome == "yes":
        validAnswer = True
        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.")

循环请求 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:
    ...

您可以简单地在整个内容中添加一个 while True:,这样,当输入 "wrong" 时,它将通过 if 子句和 return 到循环的开头,它将再次提示用户。

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