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: ")
3
  • An online tutorial like this W3Schools could help Commented Aug 27, 2019 at 16:35
  • It helped a bit but I'm still very confused. Commented Aug 27, 2019 at 16:50
  • What are you confused about? Commented Aug 27, 2019 at 18:47

3 Answers 3

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()
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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()

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.