我已经学习了10天,这是我的第一个迷你项目,我被要求建立一个登录系统为员工和1个管理员,每个有一个简单的菜单多个选项。我必须创建一个包含10名员工的txt文件,每个员工都有不同的字符(id、用户名、工资.)。员工应输入用户名并保留密码为空,但管理员应输入预定义的用户名和密码,只允许5次错误尝试。但它不工作,为管理,当我输入错误的密码,它只是5次尝试,并声明密码限制达到。
with open('data.txt') as f:
data = f.readlines()
dict = {}
for r in data:
fields = r.split(",")
id = fields[0]
username = fields[1]
date = fields[2]
gender = fields[3]
salary = fields[4]
dict[username] = {"id": id, "name": username, "date of joining": date, "gender": gender, "salary": salary}#use username as primary key since it is what employee use to log in
def log_in(username,password):
if username in dict.keys() and password=="":
print("welcome employee,you will be redirected to employee menu")
menu_employee()
elif username not in dict.keys() and password=="" :
print("Incorrect Username ")
elif username in dict.keys() and password !="":
print("Incorrect Username")
else:
for i in range(5,0,-1):
if username == "admin" and password == "admin123123":
print("welcome admin,you will be redirected to admin menu")
menu_admin()
else:
print("Incorrect Username and/or password, try again")
if i==1:
print("reached attempt limit")
else:
print("Incorrect Username and/or password, try again")
username = input("Enter a username: ")#log in menu
password = input("Enter a password: ")发布于 2022-10-30 08:17:55
您必须在for循环中再次获得密码,否则程序将继续执行所有的迭代。当您使用输入时,程序将停止,直到用户插入新密码。
def log_in(username, password):
print(dict.keys())
if username in dict.keys() and password == "":
print("welcome employee,you will be redirected to employee menu")
menu_employee()
elif username not in dict.keys() and password == "":
print("Incorrect Username ")
elif username in dict.keys() and password != "":
print("Incorrect Username")
else:
for i in range(5, 0, -1):
password = input("Enter a password: ")
if username == "admin" and password == "admin123123":
print("welcome admin,you will be redirected to admin menu")
menu_admin()
else:
print("Incorrect Username and/or password, try again")
if i == 1:
print("reached attempt limit")
else:
print("Incorrect Username and/or password, try again")我还建议您从所阅读的输入中删除所有空格,以避免出现问题。
with open('data.txt') as f:
data = f.readlines()
dict = {}
for r in data:
# Remove all white spaces
r = re.sub('\s+', '', r)
fields = r.split(",")
id = fields[0]
username = fields[1]
date = fields[2]
gender = fields[3]
salary = fields[4]
dict[username] = {"id": id, "name": username, "date of joining": date, "gender": gender,
"salary": salary} # use username as primary key since it is what employee use to log in发布于 2022-10-30 08:21:19
更新password块内的for值
for i in range(5,0,-1):
if username == "admin" and password == "admin123123":
print("welcome admin,you will be redirected to admin menu")
menu_admin()
else:
print("Incorrect Username and/or password, try again")
password = input("Enter a password: ")https://stackoverflow.com/questions/74251319
复制相似问题