-1

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

JD245
  • 61
  • 6

2 Answers2

1

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

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

Then convert it explicitly to str:

file.write('Date, Question, Name, Score\n' + str(date) + ',' question + ',' + name + ',' + str(score) + '\n')
runDOSrun
  • 10,359
  • 7
  • 47
  • 57