7/9 The Variance: Error and I am not sure why


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

print grades_variance(grades)

Not quite sure what is wrong with my code. Even after looking at all the other posts I wasn't able to pinpoint where my error is.
Error I Received: "Oops, try again. grades_variance([4, 4, 0, 9, 0, 2, 3, 1, 1]) returned 0.3433531203 instead of the expected: 7.1111111111"


#2

Hi @davidace5 ,

The indentation of your code is not being displayed accurately, making it hard for us to debug it.

However, it is likely that in the code that you submitted to the Python interpreter, the
grades_variance function was formatted as follows ...

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

If so, you would get the result that the Codecademy message reported. The problem seems to be that this statement is indented too much, making it part of the for loop ...

variance /= float(len(scores))

If that is the case, then remove one level of indentation from that statement, so that your function appears as follows ...

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

#3

Wow, I completely missed that. Thank you for your help! How can I format my code so it appears like yours when I post?


#4

Your formatting is totally fine for the record. Thanks.

You can specify a language for syntax highlighting, so here you'd do:

```python
def my_function(x): return x + 5
```

It'll also highlight without specifying language, not sure how that works. If I don't want highlighting I specify the language as text

def my_function(x): return x + 5

@geomaticmashups didn't specify the language, it looks like python was correctly guessed. But uhm. Doesn't always turn out very good.


#5

I really appreciate the help, thank you! :smile:


#6

Hi @ionatan ,

The reason I thought the indentation of @davidace5 's code wasn't displaying accurately was the fact that this statement is displayed as not being indented at all ...

return variance

... but the message cited was "Oops, try again. grades_variance([4, 4, 0, 9, 0, 2, 3, 1, 1]) returned 0.3433531203 instead of the expected: 7.1111111111".

If, in fact, that return statement was not indented in the code that was submitted to Codecademy, the Python interpreter should have complained that there was a return statement outside a function.


#7

Oh. You're you. That makes a lot of sense.

And oh. I didn't see that, or even read your post, I meant that the syntax highlighting doesn't matter.
Yes, agreed, that return is out of place in the post.


#8

float(len(scores))
The float() is not necessary, right?


#9

Hi @ajaxpro82765 ,

For this exercise, you do not actually need to use the float function in the expression that you identified, since the previous statements call functions, such as grades_average, that return floats. That makes the variance a float, which guarantees that float division would be performed here, with the call to float having been removed ...

variance /= len(scores)

#10

Hi All,

Please help me with the below error.

I am getting this error "Oops, try again.
grades_variance([2, 1, 2, 4, 7, 3, 0]) returned 620.4175824176 instead of the expected: 4.4897959184"

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(grades)
    tot_variance = 0
    variance = 0
    for each in grades:
        variance = variance + (average - each) ** 2
    tot_variance = variance/len(scores)
    return tot_variance 
print grades_variance(grades)

#11

I think this is not necessary


#12

I have removed tot_variance, but I am still getting this error

" Oops, try again.
grades_variance([4, 8, 3, 6, 0, 1]) returned 149.2317554241 instead of the expected: 7.5555555556"
please help

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(grades)
    for each in grades:
        variance = 0
        variance = variance + (average - each) ** 2
    tot_variance = variance/len(scores)
    return tot_variance 
print grades_variance(grades)

#13

It's working now. Thank you for the help.

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 each in scores:
        variance = variance + (average - each) ** 2
    tot_variance = variance/len(scores)
    return tot_variance 
print grades_variance(grades)

#14

A post was split to a new topic: Why doesn't this work?


#15