0
votes

I am trying to compare user input to an existing dictionary and keep getting value errors. I have repeatedly searched and changed the code but keep getting the same type of error. any ideas on how to solve this. I just want to verify that a username and password exist in the dictionary. Error I am getting:

File "C:\Users\jeff\Desktop\JAMGR\jeff\theCards.py", line 262, in login if NAME == users[NAME] and PASSWORD == users[PASSWORD]: KeyError: 'jeff001'

def login(entUsername, entPassword):
    NAME = entUsername.get()
    PASSWORD = entPassword.get()
    #print(NAME, PASSWORD) this was to test the stored values (actually works)
    userList()
    if NAME == users[NAME] and PASSWORD == users[PASSWORD]:
        cardCreator()

    else:
        existingUser()

'''Checks their credentials against the ones we have in the data base'''
2

2 Answers

1
votes

Check in the dictionary, and make sure that your dictionary is layed out as it is supposed to be... If i were using your app, this is exactly how your dictionary should look like:

users = {
    "Viraj": "Viraj",
    "myPassword": "myPassword"
}

notice how the dictionary key, and value are identical, that may be the issue!.

0
votes

I believe you are checking your dictionary incorrectly. Assuming your dictionary looks like this. You can't get the key by using the value (with simple a operation) since dictionaries are not designed that way. What you want to do is.

d = {'username': 'password'}
NAME = 'username'
PASSWORD = 'password'

if PASSWORD == d.get(NAME):
    # Correct ...
else:
    # incorrect ...

I use d.get(NAME) as apposed to d[NAME] because if no key exists within the dictionary that matches what is given, you get an error. However dict.get() returns None if no matching value is found which results in False for your if statement.