Iterating over a list will return the item in the list, not the index of the item. The correct code as you have written it would look like this:
def grades_sum(grades):
total = 0
for grade in grades:
total += grade
return total
Of course as others have answered this can be done much more elegant using sum
.
If you actually need the index for something else you could use enumerate
like this:
def grades_sum(grades):
total = 0
for i, grade in enumerate(grades):
total += grade #or grades[i]
return total
Or if you don't care about retrieving the item at all
def grades_sum(grades):
total = 0
for i in range(len(grades)):
total += grades[i]
return total
sum
, because that's the name of a built-in function, and it can lead to confusion.