So I’m super new to Python, and I thought a good first program would be a sort of login project. Essentially it just takes the user input and puts it as key:value in a dictionary. I’ve run into a couple main issues.
-
When the program is run and the user is asked to log in or sign up, if the user makes an account they can then login to that account after. But the second they exit the program and try to login to the account again, the account doesn’t exist. If I put the Key:Value directly into the dictionary it can then be used as an account and is saved.
-
I want to be able to save the account in a separate file so that it isn’t completely visible everytime the IDLE is opened. I am trying to pickle it, and that seems to work.
For instance, if the username is “John” and the password is “Doe” this is what is saved in the
file: ��}��John��Doe�s.
My main question is how can I now make it so that if the program is run again, the user can type “John” and “Doe” and successfully login?
Thanks.
import pickle
users = {}
status = ""
def mainDisplay():
status = input("Do you have an account?: y/n? Press ! to quit: ")
if status == "y":
existUser()
elif status == "n":
newUser()
elif status == "!":
quit()
def newUser():
createAccount = input("Please create a username: ")
if createAccount in users:
print("\nUsername already taken.\n")
else:
createPass = input("Create a password")
users[createAccount] = createPass
print("\nUser created\n")
pickle.dump(users, open("passwords.txt", "wb"))
def existUser():
login = input("Enter your username: ")
passw = input("Enter your password: ")
if login in users and users[login] == passw:
print("\nLogin successful.\n")
else:
print("\nError: invalid username or password and/or invalid combination.")
while status != "!":
mainDisplay()