9. Review, can't understand


#1




can someone say me what is wrong with my code? I can't fint out what is wrong with it?
Error : Oops, try again. It looks like not all grades were printed! Make sure you are printing ech one on their own line.


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
    return variance / len(scores)
    

def grades_std_deviation(variance):
    return variance**0.5

variance = grades_variance(grades) 
grades_std_deviation(variance) 


print grades

print grades_sum(grades)

print grades_average(grades)

print grades_variance(grades)

print grades_std_deviation(variance)


#2

Should this be a call to the function, instead?

print_grades()

#3

I don't think so
(20 chars)


#4

Ok, I've got this. I had to use a loop and print every grade in new lane
(couldn't they write it?...)


#5

Your print_grades() function has a loop. Just pass in the grades:

print_grades(grades)

This is probably what the SCT is running.


#6

I have a similar problem. The console shows a correct answer but the pop up says "Oops, try again. It looks like the standard deviation was not printed! Make sure you are printing it on its own line."

My code is:
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
return variance / len(scores)

def grades_std_deviation(variance):
return variance ** 0.5

variance = grades_average(grades)
print "all of the grades", print_grades(grades)
print "sum of grades", grades_sum(grades)
print "average grade", grades_average(grades)
print "variance", grades_variance(grades)
print "standard deviation", grades_std_deviation(variance)

I have no idea why the pop up thinks Standard Deviation is not working. Any suggestions?


#7

Shouldn't it be:
variance = grades_variance(grades)?


#8

Hi acrodan, I think we pass "grades" via variance: "variance = grades_average(grades)" I suspect the problem may be how the program assesses submissions as I think the code is right. However, I am a learner so could be wrong (:wink:


#9

i'm a learned too and I've just checked our codes, and that was one of our codes difference, so I thoght it could be it


#10

Hi acrodan, I tried your suggestion before replying and it did not work so I was convinced my first suspicion was correct; the code is good but it somehow does not work with the assessment check (which is fine so long as it lets me get to the next problem) Thanks for helping !!


#11

While we seek out the obvious (which anybody could be blinded to from too much looking) let's pick on the secondary, other functional code.

def grades_average(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

The above is complicated by two superfluous elements. The unneeded variables, and the float(). The data contains a float (50.5) so the entire program will be running with floats.

As an aside, where the float is applied runs counter to the definition of a counting number, which len() returns. If we must apply float() let it be on the numerator in a fraction such as this. Code makes sense that way.

def grades_average(grades):
    return grades_sum(grades) / len(grades)

The return value from this is the actual variance, not the accumulator inside the function. I would not use that variable name, unless it was assigned the return of this function.

variance = grades_variance(grades)

Inside the function, total suffices for sum_of_difference_in_n_squares. It's semantic. Apart from that the function is bang on.

Recall that variance is the square of the standard deviation, sigma. So we can call it 'sigma squared', again, just for semantics...

def grades_std_deviation(sigma_squared):
    return sigma_squared ** 0.5

This way the code describes itself so less manual documentation is required. As for passing the lesson, use only the variables assigned unless you are free to create them. My rant may be disregarded where superceded. It's just stuff to think about.

Okay, now to the obvious:

variance is never defined in the scope of this caller.


#12

Hi mtf,

If I do not convert len(grades) to a float I get a problem as it returns and integer but is expecting a float. I kept what I had before and that part worked.

I changed my standard deviation function to yours and that worked but I am not sure why as the variable name in a function should not really matter. Anyway, it worked so I am very grateful for your help

I can also see your comments about unnecessary variables and I guess this is a classic case of a learner making things more complex than they need be (:wink:

Thanks for your help

Best wishes

Roy


#13

Actually, this is a significant point that many of the exercise instructions do not make clear ...

Since the grades list contains ints and a float, it implies that the functions in this exercise should be able to handle lists that contain either of these types, which might be 1) all floats, 2) all ints, or 3) a mixture of floats and ints. Since any of these situations might arise, we should apply the float built in function somewhere within the grades_average function. That brings up the question of where we should do this.

Good programming style would have us apply the float function where it is reasonable to expect a float to occur, which would be the sum of the list of numbers, rather than where we have a count of how many numbers the list contains. Accordingly, good programming style would have us do this ...

average = float(sum_of_grades) / len(grades)

One reason to make it a general principle to do it this way is that in a more complex expression that involves many different operations, such as additions, subtractions, multiplications, and divisions, etc., the order of operations may affect the outcome. Sometimes an expression that ultimately returns a float might rely on performing integer division in one or more of its subexpressions. In that case, it does matter where we apply the float function. It seems best to make this function an example of where to do that.


#14

Thanks appylpye, I think I understand what you are advising. If the intention of "len(grades)" is to produce an integer then keep to that intention. So I assume dividing a float by an integer always return a float?


#15

A point I wanted to raise, but left it out so as not to dilute my post any further. My intention was to bring this up later. Thank you for taking the ball.

Since this is Python 2, the advice is good about 'any list' and using explicit declaration rather than depending upon the presence of a float in the data set. Then Python 3 comes along and makes everything a float unless we declare it an INT. There are two sides to this coin, so I think it is good that we have covered it from both sides.

You assume correctly. In Python 2,

int / int  yields an int

float / int yields a float

Any operation will result in the same, adding, subtracting, dividing or multiplying.

int _ int => int

float _ int => float

That we were able to expect a float in the earlier example is due to a float being present in the data. This is referred to as implicitly declared. We knew what to expect in the data so could justifiably skip the conversion step. But best practice is to use explicit declaration that will work with all lists, as @appylpye mentions above.


#16

Thanks mtf, very helpful !!
Take care,
Roy


#17

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