money_list
アレイをループしてみませんか?
いずれにせよ、金額を入力して、それに相当する金額を変更できるようにしたいと思いますか?
これが私がそれをする方法です:
#!/usr/bin/python
#coding=utf-8
import sys
#denominations in terms of the sub denomination
denominations = [5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5, 2, 1]
d_orig = denominations[:]
amounts = [ int(amount) for amount in sys.argv[1:] ]
for amount in amounts:
denominations = [[d] for d in d_orig[:]]
tmp = amount
for denomination in denominations:
i = tmp / denomination[0]
tmp -= i * denomination[0]
denomination.append(i)
s = "£" + str(amount / 100.0) + " = "
for denomination in denominations:
if denomination[1] > 0:
if denomination[0] >= 100:
s += str(denomination[1]) + " x £" + str(denomination[0] / 100) + ", "
else:
s += str(denomination[1]) + " x " + str(denomination[0]) + "p, "
print s.strip().strip(",")
そしてターミナルから。
$ ./change.py 1234
£12.34 = 1 x £10, 1 x £2, 1 x 20p, 1 x 10p, 2 x 2p
または確かにそして数の数
$ ./change.py 1234 5678 91011
£12.34 = 1 x £10, 1 x £2, 1 x 20p, 1 x 10p, 2 x 2p
£56.78 = 1 x £50, 1 x £5, 1 x £1, 1 x 50p, 1 x 20p, 1 x 5p, 1 x 2p, 1 x 1p
£910.11 = 18 x £50, 1 x £10, 1 x 10p, 1 x 1p