Variance


#1

I’m having issues with 16.Exam Statistics -> 7. The Variance.

My code gives the following error:

Traceback (most recent call last):
File “python”, line 26, in
File “python”, line 23, in grades_variance
TypeError: unsupported operand type(s) for /: ‘float’ and ‘list’


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
    total_variance = variance / scores
  return total_variance

print grades_variance(grades)



I don’t understand why it’s not working. Could someone explain it to me so I can fix my code?


#2

you only want to calculate the total_variance after the loop, not each iteration of the loop

scores is a list, so you need to divide the variance by the length of the list


#3

Thank you.
It worked.


#4