我正在尝试将程序扩展为将登录详细信息存储在字典中的程序,当您尝试登录时,它应该询问您的用户名和密码,以及登录详细信息是否与附加的内容相匹配到字典,它会说“你登录了!”,但我似乎根本无法调用我的login()函数,除了在没有输入登录详细信息但是没有循环回来时打印出来的print语句菜单()就像它应该的那样,因为我把它放在一个while循环中。我觉得我在主程序中搞砸了我的while循环,但似乎无法绕过它。帮助将不胜感激。
store = {}
def menu():
mode = input("""Hello, below are the modes that you can choose from:\n
##########################################################################
a) Login with login details
b) Register login details
To select a mode, enter the corresponding letter of the mode below
##########################################################################\n
> """).strip()
return mode
def login():
if len(store) > 0 : #user has to append usernames and passwords before it asks for login details
print("Welcome to the login console")
while True:
username = input ("Enter Username: ")
if username == "":
print("User Name Not entered, try again!")
continue
password = input ("Enter Password: ")
if password == "":
print("Password Not entered, try again!")
continue
try:
if store[username] == password:
print("Username matches!")
print("Password matches!")
logged() #jumps to logged function and tells the user they are logged on
break
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(): #example where the username is appended. Same applies for the password
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 \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.
store[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"]:
menu()
break
else:
validinput = False
register()
return register
def logged():
print("----------------------------------------------------------------------\n")
print("You are logged in!")
#Main routine
#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 = True
if chosen_option in ["a", "A"]:
login()
elif chosen_option in ["b", "B"]:
register()
else:
print("""That was not a valid option, please try again:\n """)
validintro = False
答案 0 :(得分:1)
这是因为在打印“您没有存储用户名和密码!”后,您将返回到外部,但永远不会将validintro更改为false。这会导致你的while循环结束。
你应该从登录中返回一个值来知道是否没有用户,并在while中检查,或者在登录功能中检查,在else部分中将global validintro设置为false(我建议第一种方式。 )