我正在努力列印輸入中的剩余金額
我試圖從成本選擇中洗掉用戶輸入的錢,但它不起作用
prod = ["Coffee", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
m = [0.1, 0.2, 0.5, 1, 2, 5, 10]
item_number = len(prod)
print("\nYour choices:")
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 enter a choice between (1-4) or hit 0 for exit: ")) -1
if choice < item_number and choice >= 0:
print("Please enter", "{0:.2f}€".format(cost[choice]), 'in total')
else:
print("Exiting the program")
exit(0)
money = float(input("Please fill the remaining amount ; "))
while money < cost[choice]:
money = float(input("You still have to enter ", cost[choice] - money))
change = money - cost[choice]
print("Your change {0:.2f}€".format(change))
uj5u.com熱心網友回復:
這有幫助嗎?
prod = ["Coffee", "Coffee with milk", "Chocolate", "Chocolate with milk"]
cost = [1.5, 1.8, 2.1, 2.4]
m = [0.1, 0.2, 0.5, 1, 2, 5, 10]
item_number = len(prod)
print("\nYour choices:")
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 enter a choice between (1-4) or hit 0 for exit: "))
-1
if choice < item_number and choice >= 0:
print("Please enter", "{0:.2f}€".format(cost[choice]), 'in total')
else:
print("Exiting the program")
exit(0)
money = float(input("Please fill the remaining amount ; "))
while money < cost[choice]:
money = float(input("You still have to enter " str("{:.2f}".format(cost[choice] - money))))
change = money - cost[choice]
uj5u.com熱心網友回復:
你的問題在這一行。
money = float(input("You still have to enter ", cost[choice] - money))
這應該是
money = float(input("You still have to enter " str(cost[choice] - money)))
首先進行計算(在括號內),然后將其轉換為字串以集中輸入字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/354737.html
標籤:Python for循环 if 语句 while 循环
上一篇:While回圈提前中斷
