I am getting the right answer for the variance , but it still gives me an error which prevents me from moving forward, can some one please tell me why I get this error ? Thanks

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(grades)
    l = len(scores)
    variance = 0 
    for i in scores:
        variance += float(average - i) ** 2
    return  variance/l 

print grades_variance(grades)


Instead of returning variance / l, you should declare a new variable before your print- statement. It would be variance divided by L. After this, return your variable.


Not a necessity, at all. The return statement is just fine.

Style guides almost universally agree with avoidance using lowercase L as variable. It is not easy to distinguish from a numeral, 1.

But this is Statistics and we do have a variable, n, a counting number to indicate number of terms in the sample space.

n = len(scores)

When we get right down to it, we don't use n anywhere but the return statement, so don't need a variable at all.

return variance / len(scores)

I've already mentioned in a recent topic on this unit that the variable, variance is possibly not the best choice on the part of the course author. The return value (a rational) is the variance; the sum of the squares is not.

mean = grades_average(scores)
sumx = 0
for x in scores:
    sumx += (x - mean) ** 2
return sumx / len(scores)

A little revision gives us a readable and simple function that has only working variables, one static, one dynamic. Think of unnecessary variables as being like the phrase your teacher says made the paragraph more difficult to read and disseminate.

When you get to advanced topics at the end of the track you will learn a new construct that is very powerful for instances such as this. This is only a demo, so don't use it in your submitted code. Stick to the algorithms you are currently working with.

u = grades_average(scores)
return sum([(x - u) ** 2 for x in scores]) / len(scores)

print grades_variance(grades)    # 334.071005917


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