Python login limit

Viewed 551

i'm trying to implement login attempt system to my current code, but i don't know where i should tick it. Can someone suggest anything? I would like to give three attempts to login, if user fails to login, system will lock user out. I just dont know where to position the code properly.

granted = False
def grant():
    global granted
    granted = True
def login(name,password):
    success = False
    file = open("user_details.txt","r")
    for i in file:
        a,b = i.split(",")
        b = b.strip()
        if(a==name and b==password):
            success=True
            break
    file.close()
    if(success):
        print("Login Succesful")
        grant()
    else:
        print("wrong username or password")
1 Answers

The better way to do this problem is by having a JSON file instead of a txt file. You can have the file in this format:

{
"username": {
 "password": "",
 "attempts": 0,
 }
}

In the login() function increment and write the count of attempts if the password is wrong. And before the function begins read the JSON and check if the attempts value is greater than 3. If it is greater send an appropriate message else to continue the login action and ask for the password.

Your code had some minor errors which I have handled here:

import re
granted = False
def grant():
    global granted
    granted = True
def login(name,password):
    success = False
    file = open("user_details.txt","r")
    for i in file:
        if i.count(',') > 0:                    # check whether i has at least one ','
          a,b = i.split(",")
          b = b.strip()
          if(a==name and b==password):
              success=True
              break
    file.close()
    if(success):
        print("Login Succesful")
        grant()
    else:
        print("wrong username or password")
def register(name,password):
    file = open("user_details.txt","a")
    file.write( "\n"+name[0]+","+password)      # name is an array so only the first element is stored.
    file.close()
    grant()
def access(option):
    global name
    if(option=="login"):
        name = input("Enter your name: ")
        password = input("enter your password: ")
        login(name,password)
    else:
        print("Enter yor name and password to register")
        name = input("Please enter your name: ").lower().split()
        if len(name) > 1:
           first_letter = name[0][0]
           three_letters_surname = name[-1][:3].rjust(3, 'x')
           name = '{}{}'.format(first_letter, three_letters_surname)
           print(name)
        while True:
            password = input("Enter a password: ")
            if len(password) < 8:
               print("Make sure your password is at lest 8 letters")
            elif re.search('[0-9]',password) is None:
                print("Make sure your password has a number in it")
            elif re.search('[A-Z]',password) is None:
               print("Make sure your password has a capital letter in it")
            else:
                print("Your password seems fine")
                break
        register (name,password)
def begin():
    global option
    print("Welcome to Main Menu")
    option = input("Login or Register (login,reg): ")
    if(option!="login" and option!="reg"):
        begin()
begin()
access(option)
if(granted):
    print("Welcome to main hub")
    print("#### Details ###")
    print("Username:",name)
Related