Exam statistics - the sum of scores


#1

Hello.
I believe there is an error in the instructions of this exercise.

In the instructions it is written:
01. Takes in a list of scores, scores

When I put scores as the function parameter as shown bellow:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum (scores):
    total = 0
    for grade in grades:
        total = total + grade
    return total
    
print grades_sum (grades)

I get this error message:
Oops, try again. It looks like your grades_sum function doesn't properly compute the sum of all the grades! It returned 1045.5 instead of 206 for grades_sum([5, 38, 0, 40, 48, 75])

When I put grades as my parameter the code works:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum (grades):
total = 0
for grade in grades:
total = total + grade
return total

print grades_sum (grades)

Thank you for your attention
`python

P.S. I don't understand how I can put the code in this message with the right tabs(spaces)


#2

you should loop over grades, this will make your function only work for a single list. What now if you add a second function call:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum (scores):
    total = 0
    for grade in grades:
        total = total + grade
    return total
    
print grades_sum (grades)
print grades_sum([5, 38, 0, 40, 48, 75])

your function doesn't work for both lists, it should


#3

Now it does work for all lists :smiley:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum (scores):
total = 0
for grade in scores:
total = total + grade
return total

print grades_sum (grades)

I was putting "for grade in grades" instead of "for grade in scores" when the parameter was scores!
Thanks a lot :smiley:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.