4. The sum of scores


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
Code is correct but output should be less right.It’s 1045.5.
<What do you expect to happen instead?>


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

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

<do not remove the three backticks above>

Can you clarify on what your question is?

1 Like

I believe he is confused as to why the output does not return with the correct number. The code seems correct, so you want to take a look?

When your code is correct, your sum is 1045.5, but the tutorial states that the sum should be something like 525, which is obviously incorrect. This makes it so every other part of this unit will say your code is incorrect even when it actually is correct. Will you fix this? << That’s the question.

You have to return total after you print it

def grades_sum(scores):

shoud be

def grades_sum(grades):

am i right?

It doesn’t matter what the grades_sum takes when defining it, so long as it takes an argument. Like you can do
def grades_sum(ponies):
as long as when you do the loop right For x in ponies: or whatever

hey I have tried this code and it is working

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

def grades_sum(x):
    scores = []
    summ = sum(x)
    return summ

print grades_sum(grades)

The instructions specifically says not to use the sum() function

Actually, it does, in this case, because that is what codeacademy wants. What the instructions says is:

On line 3, define a function grades_sum() that does the following:

1. Takes in a list of scores, scores

Even though they say scores you need to put grades. I don’t know why but one of my teachers/coach (Or whatever you wanna call it) fixed my code by doing that and it works. It will still output the same answer, CodeAcademy wants you to do it correctly. Hope this helps :slight_smile:


They need to update this lesson to edit the phrasing. “Takes in a list of scores, scores” when I tried to use scores as the lesson suggest it kept failing just as the person that submitted this comment above. Altering my code to use ‘grades’ in place of ‘scores’ it works fine.

Yes, I agree completely. :smiley:

Hi, this has now worked for me, but please can you explain why you have to return total?
I don’t quite understand the reasoning behind it?


You have to return the total, otherwise it won’t print out anything! :slight_smile:

hello, can some one tell me what should I change my code? thanks a lot, spending on so many time on it

def grades_sum(scores):
for i in range(0,len(scores)):
print total

Hi, @karateka_l ,

When you post code, you should format it so that we can see important details, such as indentation. See How do I format code in my posts?.

Your grades_sum function displays the value of total, but what value does it return?

thank you for the information
I found where I did wrong

in the end I should write return, and at another line, I should write print grades_sum(grades) :slight_smile