-1

I'm doing a simple login system and it saves usernames and passwords in a dictionary (for now). Does closing the program delete any new user info added when running the program? I want my users to be able to log back in with the same credentials they set up when previously using the program.

import getpass

usersdict = {
    "admin" : "admin",

}

def signUp():
        print("\nTo make a new account you must create your own unique username and password\n\n***\n\n")
        while True:
                newUsername = str(input("Enter your username:\n"))
                newPassword = getpass.getpass(prompt = "Enter your password: ", stream = None)
                passConfirm = getpass.getpass(prompt = "Confirm your password: ", stream = None)
                if passConfirm == newPassword:
                        userdict[newUsername] = newPassword
                        print("\n Great! Your data has been confirmed and will now be saved to the database. To play the game restart the program then login.")
                        #Here the user data should be saved some way that makes sure it is not deleted and can be retrieved when restarting program
                else:
                        print("Please re-enter your credentials.")  
  • Does this answer your question? [How to save a dictionary to a file?](https://stackoverflow.com/questions/19201290/how-to-save-a-dictionary-to-a-file) Of course the dict variable is local to the program and every time you run it, it will be initialized. See the provided link for how to save your dict outside the program, and load it back inside every time it is run – Tomerikoo Sep 30 '20 at 09:36
  • Yes they do because it's stored in ram. You can use sqlite or a text file if not a database. depending on your use case You should also encrypt the data. – Equinox Sep 30 '20 at 09:38
  • 1
    I suggest reading https://docs.python.org/3/library/persistence.html – Daweo Sep 30 '20 at 09:38

3 Answers3

0

Whenever you run your program, your dictionary gets initialized with the content on your line 3, so it only contains admin:admin in it.

When the program is running, the dictionary and it's contents are saved in your RAM, and when the program is closed, that all gets lost - run the program again, and you're back to admin:admin.

If you want to save that data, you would have to write it to a separate file, and make the program load it in when started.

Note: Storing any type of password in plain-text is a very bad practice, and should be discouraged in even simple programs such as this.

KP123
  • 36
  • 6
0

Yes - the data is gone - unless you will persist the usersdict to the disk as file.

balderman
  • 22,927
  • 7
  • 34
  • 52
0

Yes, if you store it just in memory - it's gone. If you want to store it for using later (and you do), you can do one of the following:

  1. write it in the file (json, for example) and read it at the start of the script
  2. write it in the database (if something simple - sqlite) and read it from there
kozhushman
  • 105
  • 1
  • 8