python找零钱的方法_【新手】请教有关“找零钱”的习题。

几天前,开始学习Python,有一道习题,我编写程序后总是得不到结果。(习题在3楼贴上了)

其他的都对,最后一美分的数量总是少一个。为什么呢?

我的代码如下:

cost = float(input("How much did the item cost: "))

given = float(input("How much did the person give you: "))

change = given - cost

twenty = change // 20

ten = (change - twenty * 20) // 10

five = (change - twenty * 20 - ten * 10) // 5

one = (change - twenty * 20 - ten * 10 -five * 5) // 1

quarter = (change - twenty * 20 - ten * 10 -five * 5 - one * 1) // 0.25

dime = (change - twenty * 20 - ten * 10 -five * 5 - one * 1 - quarter * 0.25) // 0.10

nickel = (change - twenty * 20 - ten * 10 -five * 5 - one * 1 - quarter * 0.25 - dime * 0.10) // 0.05

penny = (change - twenty * 20 - ten * 10 -five * 5 - one * 1 - quarter * 0.25 - dime * 0.10 - nickel * 0.05) / 0.01

print("The person's change is %1.2f"%(change))

print("The bills or the change should be:")

print(int(twenty), "twenties")

print(int(ten), "tens")

print(int(five), "fives")

print(int(one), "ones")

print(int(quarter), "quarters")

print(int(dime), "dimes")

print(int(nickel), "nickels")

print(int(penny), "pennies")

当然,中间定义各面值纸币、硬币的公式可以用别的,这是我几经修改尝试后的最后版本。结果都一样,就是一美分的少一个。好郁闷啊!

另外,请回答的朋友们,别用条件等高级代码。

就用最最基本的加减乘除等运算符号。

我用的版本是Python 3.4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值