python-3.xpermanent

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!")

Solution

  • 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