8. Standard Deviation


#1

Everything seems fine… I cant find any spelling mistakes or anything that could be wrong. Can somebody help me out?

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

def print_grades(grades_input):
  for grade in grades_input:
    print grade

def grades_sum(scores):
  total = 0
  for score in scores: 
    total += score
  return total
    
def grades_average(grades_input):
  sum_of_grades = grades_sum(grades_input)
  average = sum_of_grades / float(len(grades_input))
  return average

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)

def grades_std_deviation(variance):
  variance = grades_variance(grades)
  return float(variance) ** 0.5

variance = grades_variance(grades)
print grades_std_deviation(variance)

grades_std_deviation(5.4722222222) returned 18.2776094147 instead of the expected: 2.3392781413


#2

Although it might not affect the outcome, this expression should be in the return statement, not inside the loop.

return variance / len(scores)

There is the source of the real problem.


#4

Yes, but that is not the problem.

This is,

def grades_std_deviation(variance):
  variance = grades_variance(grades)
  return float(variance) ** 0.5

You’re overwriting the value that the user passed in.


#5

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