Variance - incorrect answer


I get the following error message. "grades_variance([7, 4, 2, 6, 3, 9, 6, 1]) returned 1.8183296658 instead of the expected: 6.4375"

I can't see why my code is incorrect.

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):
    for score in scores:
    return variance
print grades_variance(grades)


Hi @sarahvincent,

  1. In grades_variance you need to return variance/len(grades)

  2. Inside the for loop inside grades_variance you need to have variance += ((grades_average(grades) - g) ** 2) on the first line.

And as a side note, you should try to keep scores and grades uniform if you can. It is possible to write this program using only one of them.


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