4: The Sum of Scores


#1

Here's my code:

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

def grades_sum(scores):
    total = 0 
    for numbers in scores:
        total += numbers
        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 7 instead of 105 for grades_sum([7,
35, 63])"

However I cannot understand why it doesn't add the values for the scores_list into the total.

Thanks if you can help!


#2

A function ends the moment a return keyword is reached, if the return keyword is reached in a loop, the loop will break so the function can end

So, your loop breaks in the first iteration


#3

I do understand what the problem is, but not how to fix it:
if I print the total instead of returning it, I have the correct digits on the display, but It wouldn't work if I end the loop after the print.
How could i do?


#4

first step in solving a problem, is understanding the problem. So you took a first good step

You could place return outside the loop so the whole loop can run?


#5

It works just fine, thanks for the advice!


#6

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