-1

其他一切都很好,但除了两个

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?

蟒蛇 3.3

4

2 回答 2

7

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

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

//就此而言,它也适用于 Python 2。

于 2013-08-06T02:09:11.377 回答
3

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

answer, remain = divmod(cookies, people)
于 2013-08-06T02:35:38.133 回答