pythonbreak

Can someone tell me why it says my break is outside the loop?


if login1 == username and login2 == password:
    print("Welcome back " + name + " " + surname)
    break
else:
    print("Access Denied.")

Solution

  • Alright, I think the code needs a few more steps to make it work. If I understand it well, you want the user to sign up for a new account using username and password. Then, you need to verify it to see if it's correct.

    Here is a sample:

    #*********** BEGIN ACCOUNT CREATION ***************
    msg = "Invalid entry. Try again."
    print("Welcome to Python Account!")
    while True:
        # ***** THIS BLOCK CREATES NEW USERNAME
        try:
            username = input("Create New Username: ") #create new username
        except ValueError:
            msg
        else:
            try:
                username2 = input("Confirm username: ") # confirm username
            except ValueError:
                msg
            else:
                if username2 != username: # if no match, try again
                    print("Usernames do not match. Try again.")
                    continue
                else:
                    print("Username created!")
                    
                    #THIS BLOCK CREATES NEW PASSWORD
                    while True:
                        try:
                            password = input("Create a new password: ")
                        except ValueError:
                            msg
                        else:
                            password2 = input("Confirm password: ")
                            if password2 != password:
                                print("Passwords don't match. Try again.")
                                continue
                            else:
                                print("Password created!")
                                break
                        break
        break
    # ****************** END ACCOUNT CREATION ******************
    
    
    # # ****************** BEGIN ACCOUNT LOGIN *****************
    print("PLEASE LOG INTO YOUR ACCOUNT")
    attempts = 5 # this will allow 5 attempts
    while attempts > 0:
        attempts -= 1
        try:
            usrnm = input("Enter username: ")
            pwd = input("Enter password: ")
        except ValueError:
            msg
        else:
            if usrnm.isspace() or pwd.isspace():
                print("Space not allowed.")
                continue
            elif usrnm == '' or pwd == '':
                print("Value needed.")
                continue
            elif usrnm != username2 or pwd != password2:
                print("Credentials do not match. Try again.")
                print(f'You have {attempts} attempts left')
    
                if attempts == 0: # once user exhausts attempts, account is locked
                    print("You have been locked out of your account")
                    break
                continue
    
            else:
                print(f"Welcome {username2}!")
                break
        break
    # *************** END ACCOUNT CREATION *************
    

    WITHOUT TRY/EXCEPT

    #*********** BEGIN ACCOUNT CREATION ***************
    msg = "Invalid entry. Try again."
    print("Welcome to Python Account!")
    while True:
        # ***** THIS BLOCK CREATES NEW USERNAME
    
            username = input("Create New Username: ") #create new username
            username2 = input("Confirm username: ") # confirm username
    
            if username2 != username: # if no match, try again
                print("Usernames do not match. Try again.")
                continue
            else:
                print("Username created!")
                
                #THIS BLOCK CREATES NEW PASSWORD
                while True:
                    password = input("Create a new password: ")
                    password2 = input("Confirm password: ")
                    if password2 != password:
                        print("Passwords don't match. Try again.")
                        continue
                    else:
                        print("Password created!")
                        break
                break
    # ****************** END ACCOUNT CREATION ******************
    
    
    # # ****************** BEGIN ACCOUNT LOGIN *****************
    print("PLEASE LOG INTO YOUR ACCOUNT")
    attempts = 5 # this will allow 5 attempts
    while attempts > 0:
        attempts -= 1
        usrnm = input("Enter username: ")
        pwd = input("Enter password: ")
    
        if usrnm.isspace() or pwd.isspace():
            print("Space not allowed.")
            continue
        elif usrnm == '' or pwd == '':
            print("Value needed.")
            continue
        elif usrnm != username2 or pwd != password2:
            print("Credentials do not match. Try again.")
            print(f'You have {attempts} attempts left')
    
            if attempts == 0: # once user exhausts attempts, account is locked
                print("You have been locked out of your account")
                break
            continue
    
        else:
            print(f"Welcome {username2}!")
            break
    
    # *************** END ACCOUNT CREATION *************