4. The sum of scores


#1


https://www.codecademy.com/courses/python-intermediate-en-7mgOa/1/1?curriculum_id=4f89dab3d788890003000096#


I know the algorithm is correct to solve this problem, and I am wondering why the error message reads:

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 296 for grades_sum([3, 7, 73, 69, 0, 94, 1, 49])

As it stands, I can plug in these numbers, or any list of numbers produced by the error message, and the algorithm will produce the sum for the code below, so I am wondering where my disconnect in understanding is.

I expect that, given that the algorithm is correct, I should be able to move on to the next section. However, if there is really something that is missing, I would like to have a hint, or be informed. I have looked at other forums, and I thought adding a return statement would make things better. It appears not. Can someone please help me understand?


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

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

grades_sum(grades)


#2

Is it though?

The parameter is scores but you are using grades which will always add up to 1045.5. Inside the function you are destroying the parameter, which is supposed to be a list, not a value. Use a separate variable for accumuluting the sum.

Also, unless for debugging, do not print inside the function.

When we refer to an object that exists globally, the function becomes dedicated to that object and cannot take random inputs.

def add_em_up(number_list):
    total = 0
    for number in number_list:
        total += number
    return total

#3

Thank you. I suppose I was a little arrogant. Kudos mtf.


#4

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