The grades average 5 - not computing the average


#1

Could someone mabye find why the code is not working?

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 
    variance /= float(len(scores))
    return variance

Thats the error message: grades_variance([8, 3, 9, 3, 5, 5]) returned 1.0416666667 instead of the expected: 5.25


#2

we want to first add the variance of each score to variance variable

then after the loop, we want to calculate the average variance by dividing the total variance by the number of scores, aka len(scores)

you do this division:

variance /= float(len(scores))

each iteration of the loop, which is not right


#3

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