5. Computing the average


#1

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(grades)

def grades_average(grades):
    total=0
    for i in grades:
        total = total + i
        new_total = total / len(grades)
    return new_total
print grades_average(grades)

My code is getting the error "Oops, try again. grades_average([3, 8, 5, 7]) returned 5 instead of the expected: 5.75"

Anyone?


#2

try this for the average

def grades_average(grades):
    total=0
    for i in grades:
        total = total + i
    new_total = float(total) / len(grades) # you have to put this outside the for loop
    return new_total
print grades_average(grades)

#3

This function is right, but can be simplified, as follows:

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

print grades_sum(grades)

This function repeats code, rather than using the utility function above. We may simplify it as follows:

def grades_average(list):
    total = grades_sum(list)
    return float(total) / len(list)

print grades_average(grades)

#4

Supplemental:

Python has some built in methods that further simplifies the above. Consider the following:

def grades_sum(scores):
    return sum(scores) / 1.0  # converts sum to a float

print grades_sum(grades)

def grades_average(scores):
    return grades_sum(scores) / len(scores)

print grades_average(grades)

#5

Thank you everyone! Very helpful!


#6

Another short one:

def grades_average(grades):
for i in range(len(grades)):
return grades_sum(grades)/ float(len(grades))
print grades_average(grades)


#7

I didn't want to rewrite the function, this help out a lot, I got error messages when I first tried this method out, only because I forgot a "s" when I called the function grades_average. That messed with me for a good 10 minutes. Thanky thanky mtf.


#8
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

def grades_average(grades):
return float(grades_sum(grades)) / len(grades)

print grades_average(grades)
print grades_sum(grades)


#9

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 += x
return total
print grades_sum(grades)

i like to work with variables

def grades_average(grades):
i = grades_sum(grades)
ln = float(len(grades))
av = i/ln
return av
print grades_average(grades)


#10

something wrong happen with my grades_average. It running well at the console, but they say

Oops, try again.
    grades_average seems to be missing!




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)

def grades_averages(grades):
    return grades_sum(grades) / float(len(grades))
        
print grades_averages(grades)

any one could explain why ?


#11

Anything look suspicious in this?


#12

so, the eror is there because a letter 's' in my average...subhanallah..
thanks for point me that @mtf ....
now everything works like a charm..:grin:


#15