【问题标题】:python change big money into smaller moneypython把大钱变成小钱
【发布时间】:2017-10-09 15:46:07
【问题描述】:

好的,所以我有一个可以将大钱变成小钱的代码,例如:

#!/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)

有没有办法让这段代码更短

【问题讨论】:

标签: python-3.x


【解决方案1】:

is there anyway make [the code from the question shorter?]
甚至是正确的(从 perl 转换不完整?)
使用denominations1 结尾的自然数序列,以下应该有效;如果价值下降,则合理:

#!/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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2017-01-29
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    相关资源
    最近更新 更多