-2

I have the following data representing usernames and passwords in a text file named "user_table.txt":

Jane - valentine4Me
Billy 
Billy - slick987
Billy - monica1600Dress
 Jason - jason4evER
Brian - briguy987321CT
Laura - 100LauraSmith
   Charlotte - beutifulGIRL!
  Christoper - chrisjohn
Bruce - Bruce

I then create a Python dictionary using the following code:

users = {}

with open("user_table.txt", 'r') as file:
    for line in file:
        line = line.strip()
    
        # if there is no password
        if ('-' in line) == False:
            continue
    
        # otherwise read into a dictionary
        else:
            key, value = line.split(' - ')
            users[key] = value
            
k= users.keys()
print(k)

The following code is a function to authenticate using some simple rules:

a) if the user is logged in (based on login_status), the prompt

b) if the username does not exist in the file, then prompt

c) if the username exists, but does not match the password in the file, then prompt

Here is the function that (tries) to implement this:

def authenticate(login_status, username, password):


    with open("user_table.txt", 'r') as file:
        for line in file:
            line = line.strip()
        
            # if there is no password
            if ('-' in line) == False:
                continue
        
            # otherwise read into a dictionary
            else:
                key, value = key, value = line.split(' - ')
                users[key] = value.strip()
                
    user_table= users.items()
    user_names = user_table.keys()
#    print(k)

    if login_status == True:
        print('User {} is already logged in.'.format(username))

    if username not in user_table.keys():
        print('username is not found')
    
    if username in user_table.keys() and password != user_table.get('value'):
        print('the password is incorrect')
        
    if username in user_table.keys() and password == user_table.get('value'):
        print('welcome to our new application')

When I call the following:

authenticate(False, 'Brian', 'briguy987321CT')

I get this error:

AttributeError: 'dict_items' object has no attribute 'keys'

The error is caused by:

if username not in user_table.keys():

Any ideas where the mistake is?

Thanks in advance!

equanimity
  • 2,371
  • 3
  • 29
  • 53
  • 1
    Why are you using `.items()`? `users` is a dict. It has `keys` and `get` and you can easily look stuff up in it. – khelwood Jan 01 '21 at 19:45
  • Is this code just got earlier from this very S/O -site? – Daniel Hao Jan 01 '21 at 19:51
  • 1
    Does this answer your question? [Check if a given key already exists in a dictionary](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – mkrieger1 Jan 01 '21 at 19:57
  • user_table= users.items() user_names = user.keys() Here users is a dictionary and users.items() will return list and to get the list of keys use "user.keys() " not "user_table.keys()" – Abhishek Pratap Singh Jan 01 '21 at 19:59
  • In the `with`, you should use `if '-' not in line:`, because it's more readable – William Jan 01 '21 at 20:00

2 Answers2

1

user_table is not a dictionary, but rather a set-like object that holds each entry in the dictionary as a tuple (ie, {0:'a',1:'b',2:'c'}.items() becomes dict_items([(0, 'a'), (1, 'b'), (2, 'c')]). This object has no .keys() method.

I think what you should do is simply use the dictionary users:

    if login_status == True:
        print('User {} is already logged in.'.format(username))

    if username not in users.keys():
        print('username is not found')
    
    if (username in users.keys()) and (password != users.get('value')):
        print('the password is incorrect')
        
    if (username in users.keys()) and (password == users.get('value')):
        print('welcome to our new application')

(Parenthesis are just for readability)

William
  • 259
  • 2
  • 10
0

Following the suggestion of @khelwood, the following edits resolves the error message:

#    user_table= users.items()
#    user_names = user_table.keys()
    
#    print(k)

    if login_status == True:
        print('User {} is already logged in.'.format(username))

    if username not in users.keys():
        print('username is not found')
    
    if username in users.keys() and password != users.get('value'):
        print('the password is incorrect')
        
    if username in users.keys() and password == users.get('value'):
        print('welcome to our new application')
equanimity
  • 2,371
  • 3
  • 29
  • 53