Inside of a json file, each line stores the information of every different user that is created through separate classes. In the user_login
file, retrieves this info and isolates username and passwords for each user to attempt to create a login page.
File: user_login
import json
filename = "users.json"
with open(filename, "r+", encoding='utf8') as file:
'''opens json file and separates it by line by storing each line into an
array'''
lines = file.readlines()
login_info = {}
'''array that will store usernames and passwords for each user(each line in
the file is a user)'''
for line in lines:
'''simply prints each element of the lines array displaying the
information of each user'''
info = json.loads(line)
print("USER: " + str(info))
print("username: " + info["username"])
print("password: " + info["password"] + "\n")
login_info[info["username"]] = info["password"]
'''creates a new pair of username and password for each user(each line is
a user)'''
print(login_info)
print(lines)
print(login_info)
'''prompts user for their username and password'''
prompt_username = input("Please enter username: ")
prompt_password = input("Please input password: ")
The problem is in the following method(it does not work):
def login(username, password):
'''if username exists and the inputed strings match one of the key-value
pairs, login is successful'''
if username in login_info:
if password == info["password"]:
print("LOGIN SUCCESSFUL")
else:
print("Sorry, password does not exist.")
else:
print("Sorry this username or password does not exist.")
login(prompt_username, prompt_password)
How do I effectively check if the user inputs for username and password match any of the dictionary pairs to simulate a login?
The following is the users.json
file.
File: users.json
{"first": "Gilberto", "last": "Robles", "username": "girobles1", "password": "1234", "location": "San Diego", "interests": [["eat", "sleep", "code", "repeat"]]}
{"first": "Gilberto", "last": "Robles", "username": "girobles2", "password": "12345", "location": "San Diego", "interests": [["eat", "sleep", "code"]]}
It looks like you're just calling the wrong object to check if the password is correct
if password == info["password"]:
should be
if password == login_info[username]:
Follow-up question about number of attempts:
I rewrote your function to return the status of the username and password accuracy:
def login(username, password):
'''if username exists and the inputed strings match one of the key-value
pairs, login is successful returns (Username Correct, Password Correct)'''
if username in login_info:
if password == login_info[username]:
print("LOGIN SUCCESSFUL")
return (True, True)
else:
print("Sorry, password does not exist.")
return (True, False)
else:
print("Sorry this username does not exist.")
return (False, False)
login(prompt_username, prompt_password)
Then added a loop to check the results and logic to handle them (This is untested, since I don't have your dictionary):
'''prompts user for their username and password'''
username_attempts = 3
password_attempts = 3
prompt_username = input("Please enter username: ")
prompt_password = input("Please input password: ")
username_guess = 1
password_guess = 1
while True: #Loops until broken
if username_guess > username_attempts:
print("Too many invalid usernames, goodbye")
#What do you want it to do if they fail?
break
if password_guess > password_attempts:
print("Too many invalid passwords, goodbye")
#What do you want it to do if they fail?
break
username_status, password_status = login(prompt_username, prompt_password)
if all([username_status, password_status]):
break
elif not username_status:
print("Invalid username, please try again")
prompt_username = input("Please enter username: ")
username_guess += 1
elif not password_status:
print("Invalid password, please try again")
prompt_password = input("Please input password: ")
password_guess += 1