How can I compute a sum from the grades_input?

Question

How can I compute a sum from the grades_input?

Answer

Rather than computing a sum with another loop, we can make use of the function we made in the previous step: grades_sum(). If we create some variable to hold the sum of the input list, grades_input, we can assign it a value by doing our_variable = grades_sum(grades_input).
After that, computing the average can be done by following the instructions and ensuring that you make the average a float!

2 Likes

When i did this exercise i tried running it with out the float function and got the exact same result β€˜80.4230769231’.

Despite that it failed me with the message β€˜grades_average([8, 9, 4, 1, 9, 2]) returned 5 instead of the expected: 5.5’.

Why does this matter?

Maybe this article explains why. It says:

In Python 2.7, the β€œ/” operator works as a floor division for integer arguments. However, the operator / returns a float value if one of the arguments is a float (this is similar to C++)

So we have to transform an integer into float to get a float result. This is different from Python 3, by the way.

The simple fact that one of your scores was a decimal (the 50.5) automagically got you the floating result THIS TIME.

Putting the float function in insures you get a floated answer (with a decimal), instead of a rounded/ floored/integer answer.

(It appears that Codecademy background tests our coding with their own input values ----- all integers, according to your error notice ------sneaky bums :wink:)