Reputation: 13
I've just started to learn programming in Python and I'm trying to make a Login System which uses the terminal and stores data into a JSON file and retrieve it to log in.
This is the part that looks is causing the problem.
def login():
login_username = input("Enter your username: ")
login_password = input("Enter your pass: ")
with open("data.json", 'r+') as data:
if login_username == username and login_password == password:
print("Successful Login")
else:
print("Please Try Again")
login()
When I am asked for the user and pass and the input matches the data from the JSON it will loop and ask to enter the username and password, again and again.
My whole code is below
import json
uname = ""
password =""
def register():
uname = input("Enter a user to log in with: ")
password = input("Enter a password: ")
confirmed_pass = input("Enter the above password again")
if password != confirmed_pass:
print("Both the passwords does not match please re-enter a pass")
password = input("Enter a password:")
confirmed_pass = input("Enter the above password again")
login_info = {
"Username": uname,
"Password": password
}
with open("data.json", "w") as write_file:
json.dump(login_info, write_file, separators=(',', ':'))
log = input("Would you like to login? (Y/N)")
if log == 'Y' or log == "y":
login()
else:
quit()
def login():
login_uname = input("Enter your username: ")
login_password = input("Enter your pass: ")
with open("data.json", 'r') as data:
if login_uname == uname and login_password == password:
print("Successful Login")
else:
print("Please Try Again")
login()
reg = input("Have you registered (Y/N) ?: ")
if reg == 'Y' or reg == 'y':
login()
elif reg == 'N' or reg == 'n':
register()
else:
print("Error!")
Upvotes: 1
Views: 71
Reputation: 462
Based on your code, you should first use json.loads to load data in to a python dictionary:
with open("data.json", 'r') as data:
login = json.loads(data.read())
# then you can use it like this
if login_username == login['Username'] and login_password == login['Password']:
... Rest of the code
Just a heads your login will only work for last registered user, you should fix that.
Upvotes: 1