The Variance


The system would not let me pass.

Can someone please help me spot the error in my code.

def grades_variance(scores):
    for score in scores:
        variance=variance+(average - score) ** 2
    return results
print grades_variance(grades)


Hi, @ryan_beijing ,

Let's focus on this line ...


The character looks suspicious, and is not, in fact, a division operator.

You are welcome to borrow my / operator to copy and paste in its place. :slight_smile:

There is another problem on the same line, which may become more evident after the correct operator is put in place. See the new error message that appears.


Here's the new console message, which is, a lot...

Traceback (most recent call last):
File "python", line 24, in
File "python", line 22, in grades_variance
TypeError: unsupported operand type(s) for /: 'float' and 'list'


After you put in the / operator, you probably had this ...


What type of object is variance?

What type of object is scores?

Is it valid to perform a division operation with those two objects?


You're right, it is asking me to divide the number of scores, how am I able to show the number of scores.


There is a Python built in function that can determine how many items are in a list.


so,,uhh,,what's the syntax for such list?


See the documentation at Python: len(s).


I tried this: results=float('variance') / list('scores') But it says that it fails to convert string to float for variance


You have this ...

results=float('variance') / list('scores')

You should not use the quotes in the above.

scores is already a list, so you do not need to call the list function there. However, you do need to use the len function instead, to find the number of items in scores.


ok, so what should I do to the variance, it seems like that I need to find the total of it.


You already have the total, as the name, variance. You computed that in the for loop.

Now, you only need to divide it by the number of items in scores ...

results = float(variance) / len(scores)


Alright, now after the change the console returns to be correct but the system still says:

Oops, try again.
grades_variance([6, 5, 3, 8]) returned 2.1666666667 instead of the expected: 3.25


Make sure all the functions work correctly, including average.

We'll need to see your code again.


okay, the problem is solved after I changed a mistake made before. Thank you very much for your help.


Good work with the debugging. :slight_smile: