I keep getting the wrong result. Please Help!


#1

I wrote the code according to what the instructions said to do, but it keeps giving me the error

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

Please tell me whats wrong with 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(scores)
    variance = 0
    for score in scores:
        variance += ((average - score) ** 2)
        variance /= float(len(scores))
        return variance
print(grades_variance(grades))

#2

move this line

 return variance

out of the for loop


#3

How quickly these questions get answered when a post is formatted correctly.

Also, more to the OP, the last variance line should also be outside of the for loop.

variance /= float(len(scores))
return variance

#4

Did you format each line? I know that once you post your code without the correct formatting the original indents are gone therefore applying the markdown formatting will not work


#5

No. Just markdown at start and end. When code is posted without markdown, it's original state is still there. Try posting some code, but without markdown. Then copy the code from that post and paste it in a new reply, this time with backtick markdown (3 before, 3 after). Presto, chango, format restored.


#6

I moved the "return variance" outside of the for loop, but I keep getting the same error.


#7

def grades_variance(scores):
    average = grades_average(scores)
    variance = 0
    for score in scores:
        variance += ((average - score) ** 2)

    variance /= float(len(scores))
    return variance

print(grades_variance(grades))

The division by len takes place after the loop.