Home > database >  Username and password log in function with password counter
Username and password log in function with password counter

Time:10-31

I've been learning for 10 days, this is my 1st mini project, I'm asked to build a log in system for employees and 1 admin, with each having a simple menu with multiple options. I must create a txt file with 10 employees each with different charact(id, username, salary..). The employee should enter a username and leave password empty but admin should enter predefined username and password with only 5 wrong attempts allowed. but its not working as intended for admin, when I enter wrong password, it just goes for 5 attempts and state that password limit reached.

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: ")
 

CodePudding user response:

Update the value of password inside the for block

    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: ")

CodePudding user response:

You have to get again the password in the for loop otherwise the program will go ahead and will do all the iterations. When you use input the program stops until the user insert a new password

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")

I also suggest you to remove all white spaces from the input you read for avoid problems

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
  • Related