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!