0
votes

I am working on a project for my class and I am stuck on this extra credit part. I am building a password and username checker to check against a list of dictionaries that have the username and password.

I have tried many methods but cannot seem to get the result i need. The problem with the code I have is that it does not compare the password with the associated username. It results in TRUE if the password is in any dictionary. Any help would be appreciated as I will keep plugging along myself

This is what I have:

adminList = [
    {
        "username": "DaBigBoss",
        "password": "DaBest"
    },
    {
        "username": "root",
        "password": "toor"
    }
]

#Import Required Tools

import getpass


####################################
###     Define ALL FUNCTIONS     ###
####################################


#Define function to confirm login credntials of the user
def getCreds():
    user_name = input("Please input your user name: ")
    while not any(d["username"] == user_name for d in adminList):
        print("User name not recognized, please try again.")
        getCreds()

    pass_word = getpass.getpass("Please enter your password: ")
    print("Checking Credentials")


    while not any(d["password"] == pass_word for d in adminList):
        print("The password in not correct")
        pass_word = getpass.getpass("Please enter your password again: ")
    else:
        print ("Hello and welcome to the Matrix.")    

#Call the getCreds function        
getCreds()
1

1 Answers

0
votes

I will refrain from a straight coding example - but the way to go is to get both username and password and then compare both values with your admin credentials. For that task, you should probably modify your adminList from a List of dictionaries to a dictionary of dictionaries:

adminList = {"DaBigBoss": {"password": "DaBest}, "root": {"password": "toor"}}

The benefit of this arrangement is twofold: 1. It is easier to query (query if the user matches than compare the password) 2. It guarantees that there can be only one DaBigBoss,making it easier to prevent having the same username N times. (a simple if "DaBigBoss" in adminList would suffice)