- 论坛徽章:
- 0
|
请大家帮忙看看.python核心编程的一道题,我这样写为什么GetCoin函数只能执行到第一个if
第五章第5题:取一个任意小于1美元的金额,然后计算可以换成最少多少枚硬币。硬币有1美分、5美分、10美分、25美分4种。举例来说,0.76美元换算结果应该是3枚25美分,1枚1美分。类似76枚1美分,2枚25美分+2枚10美分+1枚5美分+1枚1美分这样的结果都符合符合要求的。
#! /usr/bin/python
# filename = coin.py
def GetCoin(cent) :
cent25 = divmod(cent, 25)
if cent25[1] == 0 :
return cent25[0], '25 cents coins'
else :
return cent25[0], '25 cents coins'
cent10 = divmod(cent25[1], 10)
if cent10[1] == 0 :
return cent10[0], '10 cents coins'
else :
return cent10[0], '10 cents coins'
cent5 = divmod(cent10[1], 5)
if cent5[1] == 0 :
return cent5[0], '5 cents coins'
else :
return cent5[0], '5 cents coins'
cent1 = divmod(cent5[1], 1)
return cent1[0], '1 cent coins'
while True :
a = raw_input('please input the amount of money(. to terminate): ')
if a == '.' :
break
else :
print '%s need' % a, GetCoin(int(float(a)*100))
[ 本帖最后由 e821023 于 2008-11-21 10:43 编辑 ] |
|