Krishna
Krishna

Reputation: 25

How to permanently add values to python dictionaries

I have a script that checks wether the user enters a valid username that corresponds to the correct password and grants access. it also allows them to create a username and password and adds them to the dictionary. However when I run the program and create a new username and password it adds them to the dictionary but not permanently as when the program is run again and the dictionary is printed the values I added before are no longer there.

    # creates a users dictionary
users = {
    'Joe': 'juk725',
    'Mat': 'axr3',
    'Th3_j0k3r': 'bl4z3'
}
while username not in users and username != 'signup':
    username = input("enter username(type signup to create an account): ")

    if username == "signup" or username == "Signup":
        create_username = input("enter a new username: ")
        create_password = input("enter a new password: ")

    if create_password in users:
        create_password = input("password taken re-enter: ")
    if create_username==create_password:
        create_password=input("password cannot be the same as your username re-enter: ")
    # then adds the new username to the users dictionary
    if username == 'signup':
        users[create_username] = create_password

else:
    if username in users:
        password = input("enter password: ")
        if password in users:
           print("access granted")

Upvotes: 1

Views: 816

Answers (1)

Alex
Alex

Reputation: 19104

Python only has access to the memory assigned to it by the operating system. When the Python process ends, that memory is no longer accessible. If you want more permanent storage you can write to disk.

There are lots of ways to do this but here are a couple common ones for dictionaries in Python:

  1. pickle
  2. json

Upvotes: 2

Related Questions