Variance


#1



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


It says Oops, try again. grades_variance([2, 5]) returned 5919.4097633136 instead of the expected: 2.25
Don't know where code went wrong


Replace this line with your code.


#2

Check the first line of grades_variance(scores) function(body of function).

hint**
read second point of instructions.

Extra..
Check this link for sure..


#3

Somehow it works if you put the result in a new variable
like this:
for score in scores:
variance += (average - score)**2
var = variance/float(len(scores))
return var

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

also don't forget to use float() as seen above


#4

The bottom one with just a return isn't returning a float, that will be why.

# Float in this one
var = variance/float(len(scores)) 
return var

# No float() in this one
return variance/len(scores)

Also, you could make it a float like so:

variance += (average - score)**2.0 # Adding .0 will make the output a float also.
return variance/len(scores) # This will also become a float because variance is now a float.

#5

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