划分

时间:2016-05-04 13:13:51

标签: python python-2.x

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

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

我得到333而不是333.33 怎么解决这个?我是ubuntu用户。

2 个答案:

答案 0 :(得分:0)

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

average = float(total_amount) / total_people

另一种选择是使用Python3中的后向移植除法运算符:

from __future__ import division

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

(如果你当然使用正确的变量名,这将有效。)

答案 1 :(得分:0)

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

OR

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