Variance - incorrect answer


#1



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

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):
    average=grades_average(scores)
    variance=0
    for score in scores:
        variance+=((average-score)**2)
        variance=variance/len(scores)
    return variance
print grades_variance(grades)


#2

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.


#3

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