Python 3.x Python:通过while循环调用函数
我正在尝试将我的程序扩展为一个在字典中存储登录详细信息的程序,当您尝试登录时,它会询问您的用户名和密码,如果登录详细信息与附加到字典中的信息匹配,它会说您已登录!,但我似乎根本无法调用我的登录函数,除非当我没有输入登录详细信息时,else print语句会打印出来,但它不会像预期的那样循环回菜单,因为我将它放在了while循环中。我想我在主要的例行程序中有点搞砸了我的while循环,但我似乎无法集中注意力。我们将不胜感激Python 3.x Python:通过while循环调用函数,python-3.x,function,loops,dictionary,while-loop,Python 3.x,Function,Loops,Dictionary,While Loop,我正在尝试将我的程序扩展为一个在字典中存储登录详细信息的程序,当您尝试登录时,它会询问您的用户名和密码,如果登录详细信息与附加到字典中的信息匹配,它会说您已登录!,但我似乎根本无法调用我的登录函数,除非当我没有输入登录详细信息时,else print语句会打印出来,但它不会像预期的那样循环回菜单,因为我将它放在了while循环中。我想我在主要的例行程序中有点搞砸了我的while循环,但我似乎无法集中注意力。我们将不胜感激 store = {} def menu(): mode = i
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
这是因为打印后,您没有存储用户名和密码!,您返回到外部,但不会将validintro更改为false。这将导致while循环结束
您应该从login返回一个值,以确定是否没有用户,并在while中检查,或者在else部分的login函数中检查,将全局validintro设置为false。我建议使用第一种方法。确实如此。我想如果你把最后一行和if和elsif放在同一水平上,你会没事的。但我觉得你应该把validintro的事都忽略掉。只需使用While True,添加一个菜单项q以退出,并在用户选择q时添加break。