【问题标题】:How do I create this loop? [duplicate]如何创建这个循环? [复制]
【发布时间】:2019-11-18 21:32:44
【问题描述】:

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

提前致谢

以下是我使用的代码:

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

【问题讨论】:

    标签: python python-3.3


    【解决方案1】:

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

    【讨论】:

    • 非常感谢这有帮助
    【解决方案2】:

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

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

    【讨论】:

    • 非常感谢这有帮助
    【解决方案3】:

    您可以简单地将while 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.")
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2020-09-18
      • 2021-02-21
      • 1970-01-01
      • 2012-05-01
      相关资源
      最近更新 更多