【发布时间】:2021-12-22 10:50:43
【问题描述】:
程序如下,要求选择一个产品,然后询问选择的金额,然后用户输入金额,但是如果金额与声明的值(mo)不同,程序会打印“错误硬币”,但是当用户输入正确的硬币和数量时,它应该只打印代码的“找零”部分。在我的程序中,它会打印“零钱”,然后是错误的硬币值
prod = ["Coffe", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
mo = [0.1, 0.2, 0.5, 1, 2, 5, 10]
item_number = len(prod)
print("\nPick an item:")
for number in range(0, item_number, 1):
print(number + 1,
prod [number],
'{0:.2f}€'.format(cost[number]))
print ("0 Exit")
choice = int(input("Please pick an item from (1-4) or hit 0 to exit: ")) -1
if choice < item_number and choice >= 0:
print("You should input", "{0:.2f}€".format(cost[choice]), 'in total')
else:
print("Exiting the program")
exit(0)
money = float(input("How much do you enter?; "))
while money < cost[choice]:
money += float(input("You should insert "+str("{:.2f}".format(cost[choice] - money))))
if money != mo:
print("Invalid amount.\nPlease enter a valid coin: 0.1 / 0.2 / 0.5 / 1 / 2 / 5 / 10")
else:
print("Try again")
change = money - cost[choice]
print("Change {0:.2f}€".format(change))
【问题讨论】:
-
我在您的代码中没有看到任何“恭喜”,但我认为您的缩进有点混乱。例如,
money != mo检查应该在 while 循环中。
标签: python for-loop if-statement while-loop