Python Forum
adding information to a dictionary. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: adding information to a dictionary. (/thread-22956.html)



adding information to a dictionary. - Justice_py - Dec-05-2019

So I'm creating a basic program where when it is run it asks the user if they have an account. If they enter "n" (no) then they are prompted to create a username and password. If they enter "y" they can login. the issue is that the username and password is only stored as key:value while the program is running, and is forgotten as soon as it is exited. If I manually enter into the dictionary a key:value then it is saved as an account and I can login to it. I'm trying to make it so that the account created when the user signs up is automatically put into the dictionary. Here is what I have:
users = {"John":"Doe", "GEO":"Rock"}
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")

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()
Thanks


*John:Doe and GEO:Rock are key value's that I put in just to test its ability to save an account.


RE: adding information to a dictionary. - Clunk_Head - Dec-05-2019

Looks like the new login and password is added to the dictionary on line 20. is it not working for you?


RE: adding information to a dictionary. - Justice_py - Dec-05-2019

So it adds it but only when the program is running. So after you create an account, you can then login and it will print "login successful" but as soon as you exit, you can't re-run it and login because it hasn't saved the credentials.