Oli*_*rff 1 python import currency decimal rounding
我是一个新的python,我想测试它,我的想法是制作一个脚本,这将看到你可以购买多少东西一定数量的钱.这个项目的问题是,我不知道删除小数,就像你喜欢如果你有1,99美元和苏打水花费2美元,你在技术上将没有足够的钱.这是我的脚本:
Banana = 1
Apple = 2
Cookie = 5
money = input("How much money have you got? ")
if int(money) >= 1:
print("For ", money," dollars you can get ",int(money)/int(Banana),"bananas")
if int(money) >= 2:
print("Or ", int(money)/int(Apple), "apples")
if int(money) >= 5:
print("Or ", int(money)/int(Cookie)," cookies")
else:
print("You don't have enough money for any other imported elements in the script")
Run Code Online (Sandbox Code Playgroud)
现在,如果我输入例如9,在这个脚本中,它会说我可以获得1.8个cookie,我怎么说它在输入fx 9时我可以获得1个cookie?
我怀疑你使用的是Python 3,因为你正在讨论当你将两个整数9和5分开时得到浮点数结果1.8.
所以在Python 3中,你可以使用一个整数除法运算符 //:
>>> 9 // 5
1
Run Code Online (Sandbox Code Playgroud)
VS
>>> 9 / 5
1.8
Run Code Online (Sandbox Code Playgroud)
对于Python 2,/默认情况下,运算符执行整数除法(当两个操作数都是整数时),除非您使用from __future__ import division它使其行为类似于Python 3.