Issue
I want the results to be in the format Date, Question, Name and Score
file = open("results.csv", "a")
file.write('Date, Question, Name, Score\n' + date + ',' question + ',' + name + ',' + score + '\n')
file.close()
When I run this code i keep getting the error: TypeError: Can't convert 'int' object to str implicitly
Solution
You have to cast to any ints to string string before you can concat it to another and write to file.
str(score) # <-
file.write('Date, Question, Name, Score\n' + date + ',' question + ',' + name + ',' + str(score) + '\n')
Or use str.format:
with open("results.csv", "a") as f: # with closes your files automatically
f.write('Date, Question, Name, Score\n {}, {}, {}, {}'.format(date, question, name ,score))
You may also find the csv module useful
Answered By - Padraic Cunningham Answer Checked By - Pedro (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.