-2

How do I create this loop where if welcome is not equal to "yes" or "no", it repeats the question (if they have an account), but if welcome is equal to "yes" or "no", they create an account (for "no") or allow the user to login (for "yes")?

Thanks in advance

Below is my code used :

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.")
Joe
  • 21
  • 6

3 Answers3

3

Put a loop around asking 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:
    ...
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You can add a bool variable that keeps track of user's input status (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.")
Marsilinou Zaky
  • 1,038
  • 7
  • 17
1

You can simply add a while True: to the whole thing, this way, when a "wrong" input is entered, it will fall through both of the if clauses and return to the beginning of the loop, where it will prompt the user again.

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.")
mlg556
  • 419
  • 3
  • 13