将整数除以整数给我一个浮点数

时间:2013-08-06 02:07:00

标签: python python-3.x

其他一切正常但除了两个

People = input("Number of people: ")
Cookie = input("Numnber of cookies: ")
People = int(People)
Answer = int(Cookie) / int(People)
Remain = int(Cookie) % int(People)
print ("Cookies per person: ", Answer)
print ("Cookies returning to the jar: ", Remain)

使用两个变量4(people)和11(cookies)运行代码会返回:

Number of people: 4
Number of cookies: 11
Cookies per person:  2.75
Cookies returning to the jar:  3

如何将2.75更改为2?

Python 3.3

2 个答案:

答案 0 :(得分:7)

要在此处恢复Python 2.X的int行为,您可以使用//

>>> 11/4
2.75
>>> 11//4
2

//也适用于Python 2。

答案 1 :(得分:3)

在这种情况下,您还可以使用更简洁的方法:

answer, remain = divmod(cookies, people)