7/9 whats wrong here?


#1

Hi, I have this code:

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

I get this message:
grades_variance([4, 0, 5, 1]) returned 1.0595703125 instead of the expected: 4.25

What I’m I missing from the code.

Thanks!


#2

to calculate the grade variance, we need to add the variance of grade together. This you do fine:

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

then after the loop, we need to divide the variance by the length of scores. However, you do this each iteration of the loop, which is not right

also, the function call should be outside/after the function, given the call executes the function

indent is important in python


#3

Ah, great! Yeah, need to be careful with that.

Thanks a lot!


#4