The variance


#1

This is my code

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

def print_grades(grades):
    for grade in grades:
        print grade

def grades_sum(grades):
    total = 0
    for grade in grades: 
        total += grade
    return total
    
def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

def grades_variance(scores):
    average=grades_average(grades)
    variance=0
    for i in scores:
        variance=variance+(average - i)**2
        
    variance=float(variance)/len(scores)
    return variance
    
grades_variance(grades)
    
print grades_variance(grades)

The error is:

Oops, try again. grades_variance([0, 5, 3, 0, 6, 8]) returned 5900.4354043392 instead of the expected: 8.8888888889

Can someone guide me? I am not understanding the error.


#2

your function currently only works for a single list, lets see what happens if we add a second function call:

def grades_variance(scores):
    average=grades_average(grades)
    variance=0
    for i in scores:
        variance=variance+(average - i)**2
        
    variance=float(variance)/len(scores)
    return variance
    
print grades_variance(grades)
print grades_variance([0, 5, 3, 0, 6, 8])

see how for the second function call it produces the wrong variance? Its because you use grades in the function.


#3

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