7. Variance

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

Hi, Im not passing even tough I get the results
Im supposed to get ( I tested the input which the console returns me
with a error message). I dont find a mistake… Any suggestions?

<Below this line, add a link to the EXACT exercise that you are stuck at.>
Oops, try again.
grades_variance([8, 5, 6, 8, 0, 5, 6, 3, 7]) returned 5644.2473701512 instead of the expected: 5.7777777778

<What do you expect to happen instead?>

```python

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

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

def grades_sum(grades):
total = 0
for grade in grades:
total += grade
return total

def grades_average(grades):
sum_of_grades = grades_sum(grades)
durchschnitt = sum_of_grades / float(len(grades))
return durchschnitt

def grades_variance(scores):
variance = 0
average = grades_average(grades)
for score in scores:
variance += (average - score) ** 2
total_variance = variance/float(len(scores))
return total_variance
print grades_variance(grades)

<do not remove the three backticks above>

Hint: Should you get the average of the globally defined grades, or the grades that have been passed to you as an argument scores?

average = grades_average(grades) # reconsider this line

ouuu okay… thanks!!!

A post was split to a new topic: Variance

i didnt get youwhat you have said…!?

If you call grades_average like this:

grades_average(my_scores)
grades_average(friend_scores)

where each different list passed to it gives a different output, it makes sense for grades_average to compute the average of the grades list passed to it as an argument.

this is what i was missing…

total_variance = variance/float(len(scores))

thank you :slight_smile:

I have a problem:

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

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

def grades_sum(grades):
    total = 0
    for grade in grades: 
        total += grade
    return total
    
def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

def grades_variance(scores):
    average = grades_average(scores)
    variance = 0
    for score in scores:
        variance += (average - score) ** 2
    a =  variance / scores
    return a
print grades_variance(grades)

My error is:

Traceback (most recent call last):
  File "python", line 25, in <module>
  File "python", line 23, in grades_variance
TypeError: unsupported operand type(s) for /: 'float' and 'list'

In this line, you are dividing a float by a list. That isn’t really what you’re asked to do :wink:

Do you know that i am already at the next lesson? Mute!!