0

I am trying to create a quiz which is supposed to have a registration feature that stores the user input into an external file. I just need help on how to store the user input.

This is my code:

def register():
    name = input("Please enter your full name: ")
    age = input("Please enter your age: ")
    year = input("Please enter your year group: ")
    username = (name[:3] + age)
    print("Your username is " + username)
    password = input("Please enter a password: ")
Amit Amola
  • 2,301
  • 2
  • 22
  • 37

3 Answers3

1

Programming can be confusing, all you need is to be organized.

Here is an algorithm for you.

  1. Create a file
  2. Write data to the file
  3. Close the file

And the code:

#Create a new function
def register():
    #Open a file to store the input
    with open("credentials.txt", 'a') as out:
        #Ask for the data
        name = input("Please enter your full name: ")
        age = input("Please enter your age: ")
        year = input("Please enter your year group: ")
        username = (name[:3] + age)
        print("Your username is " + username)
        #Save to the file
        out.write(username + "\n")
        password = input("Please enter a password: ")
        out.write(password  + "\n")
        print("Login created!")
        #Close the file
        out.close()    

#Entry point of your program
register()
sirandy
  • 1,834
  • 5
  • 27
  • 32
  • Thank you so much! This helped a lot. I also need to create a log in feature. I've already asked the user for input but I need it to look for their details in the text file and if it's there, they will be logged in and be moved on to the quiz. I just need help on this then I think I'll ready to go. –  Aug 27 '19 at 17:36
0

You can just write those variables to the file like this :

filehandle = open("filename", "a") filehandle.write(name + "\t" + age + "\t" + age + "\t" + year +"\t" + password + "\t") filehandle.close()

Rohit Nawale
  • 3
  • 1
  • 4
0

There are multiple ways of doing this and I'll mention them all below:

  • You can save information of all the user by using pickle where it creates a dictionary and each user can be a key and it's information as a list of values. You can also load that information back later on. Here's a link to understand more how pickle works. Check out the answers of various user: How can I use pickle to save a dict?
  • Another way is to save it in a text file as mentioned by sirandy.
  • Third way is to create a pandas dataframe and save information of each user as a new row. I am assuming you are aware of pandas dataframes. Here's a link again to explain how to do this: Writing a pandas DataFrame to CSV file
Amit Amola
  • 2,301
  • 2
  • 22
  • 37