7. Variance


#1



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?


Oops, try again.
grades_variance([8, 5, 6, 8, 0, 5, 6, 3, 7]) returned 5644.2473701512 instead of the expected: 5.7777777778


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)


#2

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

#3

ouuu okay... thanks!!!


#4

A post was split to a new topic: Variance


#5

i didnt get youwhat you have said..!?


#6

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.


#7

this is what i was missing..

total_variance = variance/float(len(scores))

thank you :slight_smile:


#8

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'

#9

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


#10

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


#11