【发布时间】: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)
有没有办法让这段代码更短
【问题讨论】:
-
is there anyway make this code shorter好吧,有三组(面额数)相似的语句,这表明另一个循环级别。然后,重复减法有一个数学抽象,在大多数编程语言中甚至是一个运算符。
标签: python-3.x