Python 用户登录后调用displayMenu()函数
用户应使用其用户名和密码登录。如果用户是管理员,那么他应该有自己的菜单和额外的选项。我遇到的问题是,成功登录后,不会调用displayMenu或displayMenu\u Admin。有一个名为“user.txt”的文本文件,其中保存了用户名和密码Python 用户登录后调用displayMenu()函数,python,function,authentication,menu,Python,Function,Authentication,Menu,用户应使用其用户名和密码登录。如果用户是管理员,那么他应该有自己的菜单和额外的选项。我遇到的问题是,成功登录后,不会调用displayMenu或displayMenu\u Admin。有一个名为“user.txt”的文本文件,其中保存了用户名和密码 #The user should be prompted to enter a username and #password. A list of valid usernames and passwords are stored in "user.t
#The user should be prompted to enter a username and
#password. A list of valid usernames and passwords are stored in "user.txt".
#Display an appropriate error message if the
#user enters a username that is not listed in user.txt or enters a valid
#username but not a valid password. The user should repeatedly be
#asked to enter a valid username and password until they provide
#appropriate credentials.
def login():
username = input("Please enter your username?:\n")
password = input("Please enter your password?:\n")
for line in open("user.txt","r").readlines():
field = line.strip().split(",")
if username == field[0] and password == field[1]:
print("Hello " + username + ", welcome back!\n")
return True
if field[0] == "admin":
displayMenu_Admin()
else:
displayMenu()
print("Username or Password Incorrect\n")
return False
login()
def displayMenu_Admin():
global menu_input
menu_input = input("Please enter one of the following options:\n r - register user\n a - add task\n va- view all tasks\n vm - view my tasks\n s - statistics\n e - exit\n")
if menu_input == "r":
register()
elif menu_input == "a":
add_task()
elif menu_input == "va":
view_all()
elif menu_input == "vm":
view_more()
elif menu_input == "s":
statistic()
elif menu_input == "e":
exit()
return menu_input
#A menu should be displayed once the user has successfully logged in.
def displayMenu():
global menu_input
menu_input = input("Please enter one of the following options:\n a - add task\n va- view all tasks\n vm - view my tasks\n e - exit\n")
if menu_input == "a":
add_task()
elif menu_input == "va":
view_all()
elif menu_input == "vm":
view_more()
elif menu_input == "e":
exit()
return menu_input
其想法是用户应该登录,如果用户是“管理员”用户,则应显示其他菜单。任何建议或帮助都将不胜感激 您的问题是,如果登录成功,您将返回,因此调用显示函数的块永远不会执行
def login():
username = input("Please enter your username?:\n")
password = input("Please enter your password?:\n")
for line in open("user.txt","r").readlines():
field = line.strip().split(",")
if username == field[0] and password == field[1]:
print("Hello " + username + ", welcome back!\n")
return True # <-- this is causing your problems
if field[0] == "admin":
displayMenu_Admin()
else:
displayMenu()
print("Username or Password Incorrect\n")
return False
但是,更好的设计可能是返回一个元组,让其他人处理菜单内容,例如:
def login():
username = input("Please enter your username?:\n")
password = input("Please enter your password?:\n")
for line in open("user.txt","r").readlines():
field = line.strip().split(",")
if username == field[0] and password == field[1]:
print("Hello " + username + ", welcome back!\n")
return True, field[0]== "admin"
return False, False
login_success, is_admin = login()
if login_success and is_admin:
displayMenu_Admin()
elif login_success:
displayMenu()
else:
print("Username or Password Incorrect\n")
上面提供的修复方法是将“返回真值”移动到after displayMenu
def login():
username = input("Please enter your username?:\n")
password = input("Please enter your password?:\n")
for line in open("user.txt","r").readlines():
field = line.strip().split(",")
if username == field[0] and password == field[1]:
print("Hello " + username + ", welcome back!\n")
if field[0] == "admin":
displayMenu_Admin()
else:
displayMenu()
return True
print("Username or Password Incorrect\n")
return False
谢谢你,尼克!非常感谢!
def login():
username = input("Please enter your username?:\n")
password = input("Please enter your password?:\n")
for line in open("user.txt","r").readlines():
field = line.strip().split(",")
if username == field[0] and password == field[1]:
print("Hello " + username + ", welcome back!\n")
if field[0] == "admin":
displayMenu_Admin()
else:
displayMenu()
return True
print("Username or Password Incorrect\n")
return False