Reputation: 1
Here is my task: Write a sign up program for an after school club, it should ask the user for the following details and store them in a file: First Name, Last Name, Gender and Form.
Here is my code so far:
f= open("test.txt","w+")
first_name = input("Enter your First name>>>> ")
last_name = input("Enter your Last name>>>> ")
gender = input("Enter your gender>>>> ")
with open("test.txt", "a") as myfile:
myfile.write(first_name, second_name, gender)
I have created the file but when I try to write to it I get an error saying
myfile.write(first_name, last_name, gender)
TypeError: write() takes exactly 1 argument (3 given)"
Upvotes: 0
Views: 632
Reputation: 10621
Following is the syntax for write() method −
fileObject.write( str )
This means you need to concat your arguments into one string.
For example:
myfile.write(first_name + second_name + gender)
Or you can use format as well:
fileObject.write('{} {} {}'.format(first_name, second_name, gender))
Upvotes: 1
Reputation: 1653
as the write function takes a single string argument, you have to append the strings into one and then write them to a file. You can't pass 3 different strings at once to myfile.write()
final_str = first_name + " " + second_name + " "+gender
with open("test.txt", "a") as myfile:
myfile.write(final_str)
Upvotes: 0