4/9 what is the problem?


#1

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

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


#2

i have the ask:
def grades_sum(scores):
total = 0
for i in scores:
total += i
return total
print grades_sum([100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5])


#3

This is what finally worked for me:

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 #could simply use total += i
****return total

print grades_sum(grades)


#4

Wrap your code in [code] [/code], it will keep the tab alignment.

Also a simpler way is to use the sum() function (mentioned in the overview of the exercise)

def grades_sum(scores):
    total = sum(scores)
    return total
print grades_sum(grades)

#5

Hi there, I also try it this way:
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

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

print grades_sum(grades)

But I keep getting an error message: "It looks like your grades_sum function doesn't properly compute the sum of all the grades! It returned 1045.5 instead of 48 for grades_sum([48])". I have no idea what I am doing wrong. Any ideas?


#6

Oops, found an error. I should have written "scores" instead of "grades" in line 5.


#7

Just FYI, the exercise mentions this "I know what you're thinking, "let's just use the built-in sum() function!" The built-in function would work beautifully, but it would be too easy."


#8

Isn't that the whole point though? To make it easier.
I mean, sure you can use a loop and do it all manually, but that's not the python way, and in general is a bad way to go about programming. The purpose of learning a language is to utilize its functions to make coding easier/more concise.


#9

same with me...nice code :grin:

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

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

print grades_sum(grades)

#11

Hi sicklad,

can you review my 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 range(len(grades)):
total += grades[i]
return total
i += 1
grades_sum(grades)

it seems to compute for first grade then exit. let me know if it is the real problem and how can i compute the whole sum instead of running some.


#12

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 += i
return total

print grades_sum(grades)

This did it for me :slightly_smiling:


#13

A post was split to a new topic: 4/9 exam statistic


#14