-4

How would I save variable such as firstname, surname and passwords in a text document from the python!

print ("Hello, stranger! What is your name?")
    firstname = input()
    print ("Welcome,", firstname, "! What is your surname?")
    surname = input()
    print ("Account registred!" "Please choose your password!")
    password = input ()
    print ("Re-type your desired password!")
    passwordre = input ()
    if str(password) == str(passwordre):
        print ("Your account has finished!")
    while str(password)!= str(passwordre):
        print ("Password don't match! Account not finished!")
        print ("Try again!")
        print ("Re-type your desired password!")
        passwordre = input ()
        if str(password) == str(passwordre):
             print ("Your account has finished")
             break
EdChum
  • 376,765
  • 198
  • 813
  • 562

2 Answers2

1

I assume from your input() that you use Python3.

Then to write in a file you can do so:

with open("filename.txt", "w") as myfile:
    myfile.write("firstname={}\n".format(firstname))
    myfile.write("surname={}\n".format(surname))
    myfile.write("password={}\n".format(password))

But I am not sure exactly what you try to achieve... You could also simply write all on a single line without descriptions:

    myfile.write("{} {} {}\n".format(firstname, surname, password))
XonqNopp
  • 101
  • 8
0

The easiest way to do it is:

with open('file\path\and\name.txt', 'w') as f:
    f.write("{firstname};{surname};{password}".format(firstname=firstname, surname=surname, password=password)

Personally I would recommend you to look at the YAML (PyYaml) module for storing data. Also, it's a bad idea to store a password unencrypted.

Matthias Schreiber
  • 2,347
  • 1
  • 13
  • 20