我正在做一个小任务。任务是编写一个自动提款机计算器。基本版本可能会产生以下输出:
Initial amount ($): 348
$100
$100
$100
$20
$20
$5
$2
$1
这是我的尝试:
#Pseudocode:
"""
Ask user for amount
For each demonination in [100, 50, 20, 10, 5, 2, 1]:
Integer-divide amount by demonination
Subtract demonination from amount to leave residual
If residual // demonination > 1, add 1 to the count of this note, and repeat
Otherwise go to the next denomination
"""
def find_denom(amount):
number_of_notes = 0
banknotes = [100, 50, 20, 10, 5, 2]
for note in banknotes:
residual = (amount // note)
if residual // note > 1:
number_of_notes += 1
return residual, number_of_notes
amount = int(input("Enter initial amount ($): "))
residual, number_of_notes = find_denom(amount)
如果我输入 348 作为初始金额,我会得到以下变量值:amount=348
、number_of_notes=3
,这对于 和 中的 100 美元纸币的数量是正确amount
的residual=174
。
我只是想让我的find_denom
功能首先工作,但我不确定从这里去哪里。