grades_sum bug?


#1

Working on the grades_sum segment and can't seem to move forward because I receiving an error code. I've copied this code into other IDE's and it works just fine. Is this a bug or..?

Here's the code:

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

def grades_sum(scores):
total = 0
for i in scores:
total = total + i
print total


#2

If you think it works just fine, then you've probably misunderstood what the code is supposed to do. I don't think the function is supposed to print anything? Read the instructions again, what is the function supposed to do?

Also, mind the indentation when you post code.


#3

got the same problem here.On returning
def grades_sum(grades):
total = 0
for grade in grades:
total = total + grade
return total

grades_sum(grades)

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

grades_sum(grades)
none of the above functions work


#4

I think you're pretty much there. If you go with the second version of your code in terms of structure, you just need to generalize the inputs from grades to score and add the print to grade_sum(grades), it'll work. My version for reference:
def grades_sum(scores):
total = 0
for i in scores:
total = total + i
return total
print grades_sum(grades)


#5

@dtran1 Thanks for the sending your code. I forgot to put the print infront of grades_sum(grades)
Thanks :smile:

print grades_sum(grades)


#6

I had the exact same issue. Can someone explain why the print needs to be added when calling the function, rather than within the function (i.e., why we can't do "print total" and then just call the function without print)? I seem to mess this up frequently so need to better understand the issue.


#7

Hello everyone here's my working code.

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

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

#8

I used this exact same code, except used i instead of score and I get this error

Oops, try again. It looks like your grades_sum function doesn't properly compute the sum of all the grades! It returned 39 instead of 236 for grades_sum([39, 90, 27, 29, 7, 18, 26])


#9

I had this same issue and it was because I used the wrong list to iterate over:

I had...

for item in grades:

instead of...

for item in scores:

HTH


#10

hahah.... have some mercy on new coders man!!


#11

Thank you!
I did not realize I need to "return" in the function, and "print" the call.


#12


#13

Return the value in the function, then print the result of the call, like this:

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

> print grades_sum(grades)