Home > Net >  How to match a string to a value in a json file
How to match a string to a value in a json file

Time:09-19

I'm trying to create a login function which gains its validation from a json file. Im asking to see if theres a way that when the user enters their login_username it will check the json file to find the data for that specific person. The logic error i'm having right now is that the user can enter any login_username and login_password and it will be working. But the logic I want is that upon entering login_username it will go to the account detail of that username so I can match that username to that password.

main.py

def login():
    login_username = input("Enter your username: ")
    valid_username = False
    login_password = input("Enter your password: ")
    valid_password = False
    #Username:
    if login_username in username_list: 
        valid_username = True
        print("Valid")
    else:
        print("Invalid username entered ")
        username_tries = 0
        while valid_username != True:
            login_username = input("Re Enter your username: ")
            username_tries = username_tries   1
            if login_username in username_list:
                valid_username = True
                print("Valid")
            if username_tries == 5 and valid_username == False:
                print("Username entered wrong too many times.")
                exit()
    #Password:
    if login_password in password_list:
        valid_password = True
        print("Valid")
    else:
        print("Invalid password entered")
        password_tries = 0
        while valid_password != True:
            login_password = input("Re Enter your password: ")
            password_tries = password_tries   1
            if login_password in password_list:
                valid_password = True
                print("Valid")
            if password_tries == 5 and valid_password == False:
                print("Username entered wrong too many times.")
                exit()
    #Confirmation:
    if (valid_username == True) and (valid_password == True):
        print("Granted Access!")
        print(f"Welcome {login_username}! ")

accounts.json

{
    "account_details": [
        {
            "Username": "testing1892437",
            "Password": "testing123",
            "Email": "[email protected]",
            "Token": "EZEEPO5A4Y"
        },
        {
            "Username": "iasodlfj123",
            "Password": "amandsf",
            "Email": "[email protected]",
            "Token": "CAFI7NEUR4"
        }
    ]
}

CodePudding user response:

Assuming you've already loaded the JSON then you can simplify your code. Use for loops to control the number of retries not forgetting the very useful for/else construct which is ideal for such cases.

data = {
    "account_details": [
        {
            "Username": "testing1892437",
            "Password": "testing123",
            "Email": "[email protected]",
            "Token": "EZEEPO5A4Y"
        },
        {
            "Username": "iasodlfj123",
            "Password": "amandsf",
            "Email": "[email protected]",
            "Token": "CAFI7NEUR4"
        }
    ]
}

reference = {d['Username']: d['Password'] for d in data['account_details']}

MAXRETRIES = 5

prompt = 'Enter username: '

for _ in range(MAXRETRIES):
    username = input(prompt)
    if (pwd := reference.get(username)):
        break
    prompt = 'Invalid username. Please try again: '
else:
    print('Too many attempts to enter a valid username')
    exit()

prompt = 'Enter password: '

for _ in range(MAXRETRIES):
    if input(prompt) == pwd:
        print(f'Access granted. Welcome {username}')
        break
    prompt = 'Invalid password. Please try again: '
else:
    print('Too many attempts to enter a valid password')
    exit()
  • Related