我最近完成了登录详细信息库程序,但最后一部分除外。
如果用户在通过register()将任何登录详细信息附加到字典之前输入“a”来调用login()来尝试访问登录选项,则应该打印“您没有存储用户名或密码!”并循环回菜单() 相反,它只是打印消息而程序停止而不是像我想的那样循环回菜单()。
我尝试在else语句上调用菜单函数但是如果我这样做,我尝试通过在菜单()中输入“b”来调用register(),该菜单在主函数的循环中运行,寄存器( )函数甚至没有被调用并突然停止就像调用login()而没有附加任何登录细节。我的其他解决方案似乎让循环工作,但代价是让菜单有更多的错误,它要求输入两次,有时根本不会调用任何东西。
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
答案 0 :(得分:1)
失败的原因是在没有用户的情况下登录(),您正在打印一个值,但没有返回任何内容。附加
return True
作为打印消息后登录功能的最后一行。所以你的登录功能应该如下所示。
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
之前没有工作的原因是你在循环条件的主函数中比较返回值'not login()'。由于我们没有返回任何值,因此将False视为默认值,not False
即True
存储在ValidIntro中。当您使用False
验证它时,此失败并退出循环。
对于最佳实践,将循环与True进行比较以避免此类情况。例如,修改循环条件以在validintro为True时运行。