4. The sum of scores


#1



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


It says
Oops, try again.
It looks like your grades_sum function doesn't properly compute the
sum of all the grades! It returned 78 instead of 320 for grades_sum([34,
9, 28, 62, 74, 11, 5, 97])


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

def grades_sum(a_list_of_grades):
    total = 0
    for i in range(int(len(grades))):
        total = total + i
    return total


#2

All this isn't necessary just simply iterate through grades.

for i in grades:

#3

Tried it but it gets this:
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 278 for
grades_sum([40, 45, 10, 61, 23, 99])


#4

you have to call grades_sum with the parameter grades

grades_sum(grades):

#5

...Wha...?
So like this?
for i in grades_sum(grades):
I get
Oops, try again.
Your grades_sum function threw the following error: maximum
recursion depth exceeded for grades_sum([55, 1, 79, 41, 34, 47, 24])


#6

No i mean in your function call

def grades_sum(grades):

#7

Oh! Well I did that now, but...:sweat:
It said I have to print grades_sum() so I put

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

buuuuut it still tells me to print it and if I unindent it it says

Traceback (most recent call last):
File "python", line 8, in
TypeError: grades_sum() takes exactly 1 argument (0 given)
so....


#8

Is your for loop indented like this,

for i in grades:
    total += i
return total

#9

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

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

Doesn't work.
File "python", line 7
return total
^
IndentationError: unindent does not match any outer indentation level


#10

No I mean like this,

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

#11

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

Of course that didn't work, since return needs to be inside the function.


#12

No no if you indent the return and for loop to the correct indentation like this,

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

I just wanted to give you something to solve. :slight_smile:


#13

Omg yay thanks~ it worked!


#14

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