7.variance


#1



hi I am stuck in average part

help me where I made mistake?


Oops, try again. Your code appears to raise an error, see the console window for the error message!


Traceback (most recent call last):
File "python", line 27, in
File "python", line 22, in grades_variance
NameError: global name 'score' is not defined


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


#2

Hi @systemcoder22323,

Your loop header is ...

    for s in scores:

... in which you defined s, but not score.

You could do this instead ...

    for score in scores:

You may want to reconsider the degree to which this statement is indented ...

        result = variance / float(len(scores))

Currently, it is part of the for loop.


#3

ya I try that before but it is not worked at that time, but now it is workly how?
thanks for the help


#4

The problem was that the variable names did not match in these two lines ...

    for s in scores:
        variance += (average - score) ** 2

The variable that you established as s in the loop header will represent each individual grade as the loop iterates. What you have as score in the next line also needs to represent each individual grade within the loop. Therefore they need to be the same variable. You can name them both score or s, or even give them another name. What is important is for you to use the same name in both places, so that they are the same variable.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.