7.The Variance


#1



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

Oops, try again. grades_variance([5, 6, 0, 2, 9, 8, 1, 7, 2]) raised an error: float division by zero


dividing the variance by the score?


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


#2

Here:

It should be:

result = variance/len(scores)

That way, you will be dividing the total variance by the number of scores rather than the score which you looped for in scores.

In full now,

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)

#3

Hi, from the instruction it said:

divide the total variance by the number of scores.

So, i think something wrong here:

    for score in scores:
        variance += (average - score) ** 2
        variance = (variance / score)  #this should be "scores"(list) instead of score
#and put it outside the loop since it asked to device the total variance
#and the total number of scores.
    return variance

something like this should work.

    for score in scores:
        variance += (average - score) ** 2
    variance = (variance / len(scores))
    return variance

Hope this help.


#4

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