Derek Phillips
Derek Phillips

Reputation: 1

Python 3 Error: TypeError: unsupported operand type(s) for +=: 'int' and 'str'

I am trying to code a Standard Deviation project and have run into an error, I will leave my code below. Not sure what exactly is causing this error, if anyone can please leave a correction or how to fix this below I would greatly appreciate it.

Billy = {
  'Homework':[76, 88, 90, 95, 54],
  'Quiz':[89, 97, 54],
  'Test':[78, 89]
}

Martha = {
  'Homework':[74, 66, 90, 100, 98],
  'Quiz':[67, 80, 99],
  'Test':[88, 98]
}

Robert = {
  'Homework':[89, 76, 65, 99, 87],
  'Quiz':[88, 98, 73],
  'Test':[81, 92]
}

#Sum
def grades_sum(homework):
    total = 0
    for grade in homework: 
        total += grade
    return total

print(grades_sum(Billy))

#Average
def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

It is returning the following errors:

Traceback (most recent call last):
  File "python", line 26, in <module>
  File "python", line 23, in grades_sum
TypeError: unsupported operand type(s) for +=: 'int' and 'str'

Upvotes: 0

Views: 691

Answers (1)

Nicolas M.
Nicolas M.

Reputation: 1478

You cannot simply call grades_sum(Billy). Billy is a dictionnary and you need a list

you can either do:

grades_sum(Billy['Homework'])

or

def grades_sum(student, key):
    total = 0
    for grade in student[key]: 
        total += grade
    return total

grades_sum('Billy', 'Homework')

I hope it helps,

Upvotes: 1

Related Questions