除法时得不到小数

Decimal not getting while dividing

我是 python 的新手。 我划分了两个数字,但我没有得到小数部分。

amount = 1000
people = 3
average = total_amount/total_people
print average

我只得到 333 而不是 333.33 如何解决这个问题?我是 ubuntu 用户。

在 Python2 中,您应该将其中一个数字转换为浮点数:

average = float(total_amount) / total_people

另一种选择是使用从 Python3:

向后移植的除法运算符
from __future__ import division

amount = 1000
people = 3
average = total_amount / total_people
print average
>> 333.333333

(当然,如果您使用正确的变量名,这将起作用)。

amount = 1000
people = 3
average = float(amount)/float(people)
print average

from __future__ import division
amount = 1000
people = 3
average = amount/people
print average