该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
# 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