2

I have recently finished my login detail vault program except for one last part.

If the user were to try to access the login option by entering "a" to call login() before appending any login details to the dictionary through register(), it should print "You have no usernames or passwords stored!" and loop back to the menu().
Instead it simply just prints the message and the program stops instead of looping back to the menu() like I wanted to.

I tried calling the menu function on that else statement but if I do this, and I try to call register() by entering "b" in the menu() which runs in a loop in the main function, the register() function isn't even called and stops abruptly just like calling login() without any login details appended. My other solutions seemed to get the loop working but at the cost of having the menu having more bugs where it asks for input twice, and sometimes not call anything at all.

vault = {}

def menu():  
    print("Welcome to the main menu")     
    mode = input("""Hello {}, below are the modes that you can choose from:\n
    ##########################################################################
    a) Login with username and password
    b) Register as a new user
    To select a mode, enter the corresponding letter of the mode below
    ##########################################################################\n
    > """.format(name)).strip()
    return mode

def login(): 
    if len(vault) > 0 : #user has to append usernames and passwords before it asks for login details
        print("Welcome {} to the login console".format(name))
        noloop = True 
        while noloop:
            username = input("Please enter username: ") 
            if username == "":
                print("Username not entered, try again!")
                continue 
            password = input("Please enter password: ") 
            if password == "":
                print("Password not entered, try again!")
                continue 
            try:
                if vault[username] == password: 
                    print("Username matches!")
                    print("Password matches!")
                    noloop = logged() #jumps to logged function and tells the user they are logged on
                    return noloop 
            except KeyError: #the except keyerror recognises the existence of the username and password in the list
                print("The entered username or password is not found!")

    else:
        print("You have no usernames and passwords stored!") 

def register(): 
    print("Please create a username and password into the password vault.\n")
    while True:
        validname = True 
        while validname:
            username = input("Please enter a username you would like to add to the password vault. NOTE: Your username must be at least 3 characters long: ").strip().lower()
            if not username.isalnum(): 
                print("Your username cannot be null, contain spaces or contain symbols \n")
            elif len(username) < 3: 
                print("Your username must be at least 3 characters long \n")
            elif len(username) > 30: 
                print("Your username cannot be over 30 characters long \n")
            else:
                validname = False 
        validpass = True 

        while validpass:
            password = input("Please enter a password you would like to add to the password vault. NOTE: Your password must be at least 8 characters long: ").strip().lower()
            if not password.isalnum(): 
                print("Your password cannot be null, contain spaces or contain symbols \n")
            elif len(password) < 8:
                print("Your password must be at least 8 characters long \n")
            elif len(password) > 20: 
                print("Your password cannot be over 20 characters long \n")
            else:
                validpass = False #The validpass has to be True to stay in the function, otherwise if it is false, it will execute another action, in this case the password is appended.
        vault[username] = password 
        validinput = True 
        while validinput:
            exit = input("\nEnter 'end' to exit or any key to continue to add more username and passwords:\n> ")
            if exit in ["end", "End", "END"]: 
                return 
            else:
                validinput = False 
                register()
        return register 

def logged(): 
    print("----------------------------------------------------------------------\n")
    print("You are logged in")

#Main routine
print("Welcome user to the password vault program")
print("In this program you will be able to store your usernames and passwords in password vaults and view them later on.\n")
validintro = False 
while not validintro:
    name = input("Greetings user, what is your name?: ")
    if len(name) < 1: 
        print("Please enter a name that is valid: ")
    elif len(name) > 30: 
        print("Please enter a name with no more than 30 characters long: ")
    else:
        validintro = True 
        print("Welcome to the password vault program {}.".format(name))

#The main program to run in a while loop for the program to keep on going back to the menu part of the program for more input till the user wants the program to stop
validintro = False 
while not validintro: 
        chosen_option = menu() #a custom variable is created that puts the menu function into the while loop
        validintro = False

        if chosen_option in ["a", "A"]: 
            validintro = not login() 

        elif chosen_option in ["b", "B"]: 
            register() 

        else:
            print("""That was not a valid option, please try again:\n """) 
            validintro = False 

1 Answer 1

1

The reason this is failing is in the login() when there are no users, you are printing a value but are not returning anything. Append return True as the last line of the login function after printing the message. So your login function should look like below.

def login(): 
  if len(vault) > 0 : #user has to append usernames and passwords before it asks for login details
    print("Welcome {} to the login console".format(name))
    noloop = True 
    while noloop:
        username = input("Please enter username: ") 
        if username == "":
            print("Username not entered, try again!")
            continue 
        password = input("Please enter password: ") 
        if password == "":
            print("Password not entered, try again!")
            continue 
        try:
            if vault[username] == password: 
                print("Username matches!")
                print("Password matches!")
                noloop = logged() #jumps to logged function and tells the user they are logged on
                return noloop 
        except KeyError: #the except keyerror recognises the existence of the username and password in the list
    print("The entered username or password is not found!")
  else:
    print("You have no usernames and passwords stored!")
    return True

The reason it didn't work earlier is you are comparing the return value 'not login()' in the main function for loop condition. As we are not returning any value, False is taken as default value and not False ie True is stored in ValidIntro. As you are validating it with False, this failed and loop exited.

For the best practice, compare the loop with True to avoid such cases. For example, modify the loop condition to run when validintro is True.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.