The Variance


#1




The system would not let me pass.


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


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


#2

Hi, @ryan_beijing ,

Let's focus on this line ...

results=variance/scores

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.


#3

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'


#4

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

results=variance/scores

What type of object is variance?

What type of object is scores?

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


#5

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


#6

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


#7

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


#8

See the documentation at Python: len(s).


#9

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


#10

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.


#11

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


#12

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)

#13

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


#14

Make sure all the functions work correctly, including average.

We'll need to see your code again.


#15

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


#16

Good work with the debugging. :slight_smile:


#17