How can I make this function return and print a float:
x = input ("Enter 5 numbers:")
def average(x):
return sum(x) / len(x)
print average(x)
How can I make this function return and print a float:
x = input ("Enter 5 numbers:")
def average(x):
return sum(x) / len(x)
print average(x)
In python 2.x, int
object divided by int
yields int
.
You should convert one (or both) of the operand to float
to get float
result:
>>> 10 / 2
5
>>> float(10) / 2
5.0
Or turn on true division using __future__
module:
>>> from __future__ import division
>>> 10 / 2
5.0