# "The Varience" - wrong result in grades_varience

#1

When I run my code as part of the lesson I get
**Oops, try again.
**

When I run it in Codecademy Labs (Python) I get the answer that the lesson says it was expecting (so in the above example if I run it with [9,5] I get the correct answer of 4.0 , not what the lesson says it got running it for me (0.6153846154).

I don't get why I seem to be getting two different results with exactly the same code, both running in different places in Codeecademy.

Can anyone see where I'm going wrong (and thanks for taking to time to help or to try to help) ?

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

total = 0
print "total: " + str(total)

print "average: " + str(average)
return average

variance = 0
for score in scores:
print (average - score) ** 2
variance += ((average - score) ** 2)
print variance

#2

Here you need to use len(scores) not len(grades).

#3

You were absolutely right !
Thanks for the help, all the best.

#4

A side note:

When faced with a choice between making a computed value or a counting number into a float, choose the computed value and leave the counting number as an integer.

variance += float(average - score) ** 2    # variance is a float

return variance / len(scores)                  # a float value is returned

This all can trace back to the grades list and the grades_sum() function. That the point at which to define the floats.

total += float(grade)                          # total will be a float

After that we no longer need to set a float anywhere else in the program. In truth, even the above is not necessary except for explicitness. The last value in the list is a float so even if total is an integer, adding that value will switch it to a float.

#5

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