首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有密码计数器的用户名和密码登录功能

带有密码计数器的用户名和密码登录功能
EN

Stack Overflow用户
提问于 2022-10-30 07:46:03
回答 2查看 39关注 0票数 0

我已经学习了10天,这是我的第一个迷你项目,我被要求建立一个登录系统为员工和1个管理员,每个有一个简单的菜单多个选项。我必须创建一个包含10名员工的txt文件,每个员工都有不同的字符(id、用户名、工资.)。员工应输入用户名并保留密码为空,但管理员应输入预定义的用户名和密码,只允许5次错误尝试。但它不工作,为管理,当我输入错误的密码,它只是5次尝试,并声明密码限制达到。

代码语言:javascript
复制
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: ")
EN

回答 2

Stack Overflow用户

发布于 2022-10-30 08:17:55

您必须在for循环中再次获得密码,否则程序将继续执行所有的迭代。当您使用输入时,程序将停止,直到用户插入新密码。

代码语言:javascript
复制
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")

我还建议您从所阅读的输入中删除所有空格,以避免出现问题。

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2022-10-30 08:21:19

更新password块内的for

代码语言:javascript
复制
    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: ")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74251319

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档