It's Okay to be Average? Why does total need to be a float?


From the code below:

def average(numbers):
total = float (sum(numbers))
return total/len(numbers)

So why did “total” have a be a float?

<do not remove the three backticks above>


Total needed to be a float so you would end with a decimal number.

If you did plain integer operations, 5 / 2 would be 2.
Using (Float operation) 5.0 / 2 would give the intended answer, 2.5.


To divide two integers and end up with a float, you must first use float() to convert one of the integers to a float. So, here the total is converted into float