I have recently finished my login detail vault program except for one last part where 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(), but 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. Help would appreciated as usual.
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 #Starts a loop if True will begin looping
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 #to return the noloop depending on users option (True or False)
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 #whileloop will loop the username variable and while True it runs the loop
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: #any username with more than 30 characters is rejected and looped back
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: #passwords that are less than 8 characters long are rejected and loops back
print("Your password must be at least 8 characters long \n")
elif len(password) > 20: #passwords that are more than 20 characters long are rejected and loops back
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 #the appending process of username and passwords to the dictionary
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"]: #if input matches these conditions, the loop will then break and thus jump back to the main menu
return #returns outputs to the called function
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 #Whileloop while False will run the intro in a loop
while not validintro:
name = input("Greetings user, what is your name?: ")
if len(name) < 1: #if the user enters a name less than 1 character, it is a null and asks for name again
print("Please enter a name that is valid: ")
elif len(name) > 30: #the name entered by user cannot exceed 30 characters and any more will be considered as invalid and will ask for name again
print("Please enter a name with no more than 30 characters long: ")
else:
validintro = True #whileloop while True will exit out of the while loop and welcome the username to the program
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 #whileloop while False runs the program in a while loop
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"]: #if statement checks if input matches these conditions
validintro = not login() #if so call login function
elif chosen_option in ["b", "B"]: #if statement checks if input matches these conditions
register() #if so call the register function
else:
print("""That was not a valid option, please try again:\n """) #Response to a null input by the user and loops back to ask for input again
validintro = False #This is set as false so that the loop through the menu can only be broken by the if statements calling the other functions
Aucun commentaire:
Enregistrer un commentaire