0

I am trying to make a sign up system in python 3.7 but I don't know how to permanently add a users login to a list and not having to sign up for the same account every time you open the program.

I was not able to make up a solution to this problem.

Usernames = ['User1', 'User2']
Passwords = ['password123', 'password123']


print('Type SU to Sign Up or type LI to Log In!')
UOption = input('Option>> ')

if UOption == 'SU':
    SI = True
    LI = False
    if SI == True:
        print('Signing Up!')
        SUUsername = input('Username>> ')
        SUEmail = input('Email>> ')
        SUPassword = input('Password>> ')

    Usernames.append(SUUsername)
    Emails.append(SUEmail)
    Passwords.append(SUPassword)
    LI = True
    SI = False

I am expecting when I get this working that the user will be able to sign up once then be able to log in if they reopen the program without having to sign up again.

Ivan Vinogradov
  • 4,269
  • 6
  • 29
  • 39
Echo
  • 3
  • 2
  • see: https://stackoverflow.com/questions/9536714/python-save-to-file and https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list Also welcome! If you get the time checkout: https://stackoverflow.com/tour – jmunsch Jul 25 '19 at 15:31
  • also see: https://stackoverflow.com/questions/7100125/storing-python-dictionaries basically what I am trying to say is create a file to save to, and to read from when the program runs. – jmunsch Jul 25 '19 at 15:34
  • 1
    But then wouldnt all the logins be easily accessable to anyone? – Echo Jul 25 '19 at 16:16
  • 1
    Yep, and in real world applications that is exactly why they do not write the login information to a file and call it a day. At a high level what happens is when a user creates a password a one-way cryptographic hashing function is used to generate a hash (digest), and then that hash is what is stored in a file or database. Then, when the user wants to log in the same hashing function is used and the program checks if the two digests are equal. If you're interested in stuff like that some good terms to search for are password hashing, password salt, bcrypt – avern Jul 25 '19 at 16:44

1 Answers1

0

You could use the pickle module:

Firstly, to create the necessary files, run the following code in the folder that your program is saved in:

import pickle
pickle.dump([],open("usernames.dat","wb"))
pickle.dump([],open("emails.dat","wb"))
pickle.dump([],open("passwords.dat","wb"))

In your program, add:

import pickle

at the start of the program

Replace the lines:

Usernames = ['User1', 'User2']
Emails = ['Email1', 'Email2'] # I'm assuming this line has just been missed off your question
Passwords = ['password123', 'password123']

With:

Usernames = pickle.load(open("usernames.dat","rb"))
Emails = pickle.load(open("emails.dat","rb"))
Passwords = pickle.load(open("passwords.dat","rb"))

To read from the files

And finally add these lines at the end of your program to update the files with the new user's details

pickle.dump(Usernames,open("usernames.dat","wb"))
pickle.dump(Emails,open("emails.dat","wb"))
pickle.dump(Passwords,open("passwords.dat","wb"))

This is a link to more details on how to use pickles, if your interested

https://www.tutorialspoint.com/python-pickling

For source code including these edits see here:

https://pastebin.com/H4ryP6cT

John Skeen
  • 223
  • 1
  • 5
  • 9
  • https://pastebin.com/raw/M3gcsE01 that's the source to it the program I tried using pickle and it was confusion so I decided to just give you the source to make it easier for you to explain. – Echo Jul 25 '19 at 17:08
  • I couldn't seem to access the source (it might be a private post) but here is the link to the source code including the edits above (that will write the lists to files) https://pastebin.com/H4ryP6cT . I hope this helps you solve your problem – John Skeen Jul 26 '19 at 08:16