7. Variance - Nothing works?


I'm working on Lesson 7: Variance: https://www.codecademy.com/courses/python-intermediate-en-7mgOa/2/1?curriculum_id=4f89dab3d788890003000096

The error message I'm getting is this:

Oops, try again.
grades_variance([0, 8, 0]) returned 14 instead of the expected: 14.2222222222

I've run it several times, and the list in the error message will have more or less items, such as

Oops, try again.
grades_variance([6, 5, 7, 5, 6, 3, 5, 3]) returned 1 instead of the expected: 1.75


Oops, try again.
grades_variance([5, 4]) returned 0 instead of the expected: 0.25

Here's my code, which I've literally copied from another thread to be sure it's correct:

[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 / len(grades)
    return average

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

Am I doing something wrong, or is this some kind of glitch? I have no idea how to identify a pattern in those error messages. :tired_face:


Your function, grades_average, doesn't output a float (hence why you won't have a bunch of numbers after your decimal). Simply add float to this line:

average = sum_of_grades / len(grades)
# add float so it looks like this:
average = sum_of_grades / float(len(grades))

Refer to instruction #3 in lesson 5. Computing the Average. It tells you to add float in that function. Not sure how you passed that.


I know I put float into that line originally, but it may have been some compounding of errors and adjustments that led me to this. thank you so much!!


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