python将大笔钱换成小钱

时间:2017-10-05 10:33:15

标签: python-3.x

好吧所以我有一个代码可以将大笔资金变成更小的钱,如:

#!/usr/bin/python3

money = int(input("input your money "))
m1 = 0
t5 = 0
t10 = 0

# split biggger money into smaller one
while money >= 10:
    money = money - 10000
    m10 = m10 + 1
while money >= 5:
    money = money - 5000
    m5 = m5 + 1
while money >= 1:
    money = money - 1000
    m1 = m1 + 1

# print them out
if m10$ > 0:
    print("there are %i 10$" % t10k)
if m5$ > 0:
    print("there are %i 5$" % t5k)
if m1$ > 0:
    print("there are %i 1$" % t1k)

所以无论如何都要缩短这段代码

1 个答案:

答案 0 :(得分:0)

is there anyway make [the code from the question shorter?]
甚至更正(从perl转换不完整?)
对于denominations一系列以1结尾的自然数,以下内容应该有效;合理的,如果价值下降:

#!/usr/bin/python3

money = int(input("input the amount "))
denominations = (100, 30, 10, 3, 1)

for denomination in denominations:
    count, money = divmod(money, denomination)
    if count:
        print("there are", count, denomination, "radolls")