rodude123
rodude123

Reputation: 300

how to permanently store an input made by a user in python even after the script restarts, the user input will be stored

I am a little stuck with my code I am trying to make a login system where the user can login to their account and use the commands that I have set, but i wanted add some extra input so that the user can register to the login system and use the commands I have set. I wanted to store the input made by the user permanently in a different variable each time so that when the user restarts the peice of code they can log in to the system and they wouldn't need to register again.

Here is the piece of code that I have created so far:

print ("Welcome!")
print ("Would you like to register")
loop = True

while (loop == True):
    username = input ("username: ")
    password = input ("password: ")
    print ("register here if you don't have an account")
    username1 = input ("name: ")
    print ("this is what you use to login to the system")
    username2 = input ("username: ")
    username3 = input ("password: ")

    if (username == "rohit" and password == "rodude") :
        print ("hello and welcome " + username or )
        loop = False
        loop1 = True

    else:
        print ("invalid username and password")

    while(loop1 == True):

        command = str(input(username + "{} > >"))
        if(command.lower() == "exit"):
            loop1=False

        elif(command.lower() == "hi"):
            print("Hi " + username + "!")

        else:
            print ("'" + command + "' is an invalid command!")

Upvotes: 2

Views: 10381

Answers (2)

rodude123
rodude123

Reputation: 300

Hey guys your ways are too complicated all you could do is this

    name = open("usernames.txt", "w") #opens file usernames.txt and gets ready to write to it
file = input("please type some text: ") #asks user for text in code

name.write(file) #writes contents in file to usernames.txt
name.close() #closes file
open1 = open("usernames.txt", "r") #opens file to read it
print (open1.read()) #prints whatever is in the text file

Upvotes: 1

Chris Tavares
Chris Tavares

Reputation: 30411

You can't use variables for local storage. If you want information to persist across program runs, you need to store it in a persistent location - typically a disk file or a database. There are a lot of modules available to make this easier, Pickle (as noted in klashxx's response) is an excellent one for simple scenarios.

Upvotes: 0

Related Questions