# Computing the average (Why using float?)

#1

This code is correct, but I do not understand why we use "float" as function, because the length will always be an integer.

If we are using it because of the fact that the equation may result in a float number, why did we not use it in the grades_sum function? Because "grades" list contains float numbers. This really confused my mind.

``````grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

total=0
for i in range(len(scores)):
total+=scores[i]

#2

This is a point I bring up quite often. Why declare a counting number as a float? It does not make semantic sense.

``return float(grades_sum(grades)) / len(grades)``

makes much more sense to the reader.

which the author did on purpose, one supposes so all the values will be floats. Change that float to an integer and everything falls apart. Your idea of declaring the float explicitly in `grades_sum` is absolutely correct.

``total = float(0)``

That one line will make all the data into floats so no further declaration will be needed anywhere else in the code.

#3

Firstly I should say that this is very complicated to understand to me. I understood from what you wrote that because the grades list contains a float number, it is already accepted float in the beginning so it is not needed to type float for total. But in grades_average function, why does the compiler expects me to type float(len(grades)) while we were not taught to put it in almost all other exercises. Is it because the float grades_sum(grades) can not be divided to an integer without changing it to float?

#4

It doesn't. The fact that `float()` may not have been covered yet would explain why the author `fudged` the exercise by providing a float in the data that would force the entire program to generate float results, by default. The above explains how we would do this explicitly, without assuming any data is a float.

#5

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