# 8/9 problem

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

total = 0
for score in scores:
total += score

return average

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

return variance ** 0,5

print variance

So this is my code, i have now looked over it for 10 minutes and tried out multiple thingsâ€¦
â€śgrades_std_deviation(6.6875) returned (1.0, 5) instead of the expected: 2.5860201082â€ť is the error message that i get with the code from above. when i try to test grades_std_deviation with a number like 123 or something it always delivers a list (1 , 5).

The pproblem is probably pretty stupid and i will be ashamed, but i give up after comparing my code to others and checking over and overâ€¦

#2

The code should see a . not a comma.

#3

calculate the variance of multiple grades consist of two parts:

calculating the variance of the grades in the list and adding them together. lets name this total_variance (the variable use for it is variance)

after successfully doing step 1, you need to divide the total_variance by the length (the number of grades).

And there is the problem, you do this each iteration of the loop, not at the very end after the loop

mtf explains why you get a tuple (immutable list)

#4

I did, i think ^^ i do not get why grades_std_deviation would return a list

#5

ohhh snap Thank you ill try to change that

#7

#8

you are my hero thank you!

#9

that was one of the two problems, what i mentioned was/is a problem as well

#10

Youâ€™re welcome. The check indentation point is a boiler-plate suggestion.

The problem @stetim94 refers to is in this segmentâ€¦

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

Itâ€™s actually hidden in that it does not affect the final outcome. The result will still be the sum of the squares divided by sample size. The assignment can/should be outside of the loop, and may even be returned without assignment.

return float(variance) / len(scores)

#11

oops, auto pilot. Sometimes people do:

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

then it becomes slightly a problem. Sorry for the confusion @megawhiz22605

#12

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