python换零钱有多少种方案_Python之100个项目:零钱转换程序

这是一个Python程序,用于计算购买商品后根据输入金额找零的硬币数量,包括quarters, dimes, nickels, pennies。用户输入商品价值和支付金额,程序返回找零总额及其细分硬币数量。" 7052782,1215825,Oracle Redo 日志管理与优化,"['数据库', 'Oracle', '日志管理', '性能优化']
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

# Python之100个项目#

假设一个商品的价值为x元,你给了y元, 计算找给你z元的程序(xyz任意的名称而已)。需要注意的是,找你的零钱需要转换成quarters, dimes, nickels, pennies的数量。代码仅供参考:

"""**Change Return Program** -

he user enters a cost and then the amount of money given.

The program will figure out the change and the number of

quarters, dimes, nickels, pennies needed for the change."""

#coding=utf-8

cost = float(raw_input("Enter cost of item: "))

given = float(raw_input("Enter amount given: "))

if given < cost:

print "sorry. You owe $%.2f" % (cost - given)

else:

tw,te,f,o,q,d,n,p = 0,0,0,0,0,0,0,0

dollars = int(given - cost)#整数部分

change = (given - dollars - cost) * 100 #小数部分

if dollars >= 20:

tw = int(dollars / 20)

dollars = dollars % 20

if dollars >= 10:

te = int(dollars / 10)

dollars = dollars % 10

if dollars >= 5:

f = int(dollars / 5)

dollars = dollars % 5

if dollars >= 1:

o = dollars

if change >= 25:

q = int(change / 25)

change = change % 25

if change >= 10:

d = int(change / 10)

change = change % 10

if change >= 5:

n = int(change / 5)

change = change % 5

if change >= 1:

p = change

print "Your change is $%.2f: \n \

%.0f twenties \n \

%.0f tens \n \

%.0f fives \n \

%.0f ones \n \

%.0f quarters \n \

%.0f dimes \n \

%.0f nickels \n \

%.0f pennies" % (given - cost, tw, te, f, o, q, d, n, p)

链接如下:

链接:http://pan.baidu.com/s/1eSzbkd0 密码:hylj

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值