7/9 The Variance: Code returns invalid with error


#1

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)

//// = indention because i don't know how else to show where indented

This code returns with the error "Oops, try again. grades_variance([0, 7, 6, 1, 4, 9, 2, 0]) returned 1.740537242 instead of the expected: 10.234375"
I'm not sure why this is occurring and don't know how to fix it. Can someone plz tell me how I can fix it


#2

There are a bunch of formatting tools in the text box where you enter your post. Please use those to ensure that the code looks just the way it should.

How many times are you supposed to do division in your function?


#3

variance += (average - score)**2
variance = variance / len(scores)

I think the problem is here, when you add a (average-score)**2, you divide it.
i returned variance/float(len(scores)) after you get the final variance += (average - score)**2


#4

É assim:

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


#5

Ok

  • I assume you pasted your code on the editor, thus you want it to appear clearer (more code like) on the preview console.
    So after pasting your code highlight it and press the </> button.

  • For a more elaborate feel to your code , engulf your code in three grave accents followed by python (write code) then close it with three more grave accents.

  • On your keyboard there is a tab button, that represents an indentation

so in essence my code looks like this

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

Hope it is readable for you.
Enjoy :eyeglasses:


#6

My Code:

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


#7

def grades_variance(x):
average = grades_average(x)
variance = 0
for i in x:
variance += (average - i) ** 2
result = variance / len(x)
return result
print grades_variance(grades)


#8

you are dividing inside the for cycle, do it outside (result 334.071005917 )

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

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

i managed to do smth like this and gives me 334.071005917, but there is one BIG BUT.
i write automatisch without knowing what i write . so weird. i just need to see proses with photos or cartoon characters to understand fylly .