0

I have created a piece of coding however I have begun to try to find the average of each persons score, but do not know what else to do. The code does not work:

def average():#makes function 'average'
print ("\nThe Average Score")#outputs the title 'The Average Score'
for pupils in classScore:
    pupil["total"] = (int(pupil["Pupil's Score 1"])+int(pupil["Pupil's Score 2"])+int(pupil["Pupil's Score 3"]))
    pupil["average"] = (pupil["total"]//3)
    print (pupil["Pupil's Name"]+pupil["average"])
average()

The CSV file is laid out like this:

Pupil's Name     Pupil's Score 1    Pupil's Score 2       Pupil's Score 3
Joao                  10                 9                       8
Rebecca                7                 6                       5
Snuffles               0                 1                       2

The error message that appeared was:

Traceback (most recent call last):


File "E:/Controlled Assesment Computing/Controlled Assesment/Task 3/Try 18.py", line 56, in <module>
    average()
File "E:/Controlled Assesment Computing/Controlled Assesment/Task 3/Try 18.py", line 53, in average
    print (pupil["Pupil's Name"]+pupil["average"])
TypeError: Can't convert 'int' object to str implicitly

If anyone could help it would be much appreciated.

phuclv
  • 37,963
  • 15
  • 156
  • 475
Joao
  • 53
  • 4

1 Answers1

1

The message looks clear:

TypeError: Can't convert 'int' object to str implicitly

You must have to do an operation to turn a number into a string. Try this:

print(pupil["Pupil's Name"]+str(pupil["average"]))

You'd done yourself a disservice by limiting this method to three students. You could easily make it work for any number of students.

I would advice against printing from that average method. A method should do one thing well.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I apologise, however I tend to understand things very slowly. Would it be possible if you could repeat yourself in simpler terms? – Joao Mar 29 '16 at 09:38
  • I have done as you said however it only prints out one person's name and their average, and does this three times in stead of printing out all three of the pupils names and their averages. – Joao Mar 29 '16 at 10:01