我是Python新手,我正在尝试制作一个程序,根据用户输入从银行取款。这只适用于100美元、50美元和20美元的钞票。 如果我输入60、80、110和其他值,程序将选择可用的最高账单,而银行剩下的取款不能取款
代码如下:
while True:
try:
money_amount = int(input('How much you want to withdraw? '))
if money_amount == 0:
print('Type in a valid value.')
continue
except ValueError:
print('Not accepted. Try again.')
else:
print(f'Withdraw amount: $ {money_amount:.2f}')
for bill_value in [100, 50, 20]:
bill_quantity = money_amount // bill_value # Divide saque // valor p/ encontrar quantia de cédulas
money_amount %= bill_value # Pega o resto da divisão de saque / valor. O que sobrar é calculado no próximo loop
print(f'$ {bill_value} Bills → {bill_quantity}')
if money_amount != 0:
print(f'\033[31mERROR!\033[m This bank uses only \033[33m $ 100, $ 50 and $ 20 bills!!!\033[m')
print('Try again.')
continue
break
print('\033[32mOperation Success\033[m')
如果我将值$1添加到项目列表中,操作将永远不会失败。。。 [100,50,20,1]-这是可行的,但这不是解决方案。。。 如果有人能帮助我理解为什么会发生这种情况以及我做错了什么,我将不胜感激
你的收回逻辑有一个根本性的缺陷——你从最大的去表示到最低的去表示。这不适用于您允许的有限账单
你只能换那笔钱
无法更改任何其他输入。您可以相应地编码:
输出:
相关问题 更多 >
编程相关推荐