我做了一個購物程式,我試圖獲取插入相應產品的數量的值,但它只列印最后輸入的值并覆寫所有以前的值
final_option, total_quantity, total_cost, total = 0, 0, 0, 0
cart = []
while continue_order != 0:
option = int(input("Which item would you like to purchase?: "))
cart.append(option)
if option >= 10:
print('we do not serve that here')
elif option == 1:
quantity = int(input("Enter the quantity: "))
total = quantity * 150
print("The price is: ?" str(total))
elif option == 2:
quantity = int(input("Enter the quantity: "))
total = quantity * 120
print("The price is: " str(total))
elif option == 3:
quantity = int(input("Enter the quantity: "))
total = quantity * 60
print("The price is: " str(total))
elif option == 4:
qquantity = int(input("Enter the quantity: "))
total = quantity * 180
print("The price is: " str(total))
elif option == 5:
quantity = int(input("Enter the quantity: "))
total = quantity * 150
print("The price is: " str(total))
elif option == 6:
quantity = int(input("Enter the quantity: "))
total = quantity * 90
print("The price is: " str(total))
elif option == 7:
quantity = int(input("Enter the quantity: "))
total = quantity * 150
print("The price is: " str(total))
elif option == 8:
quantity = int(input("Enter the quantity: "))
total = quantity * 120
print("The price is: " str(total))
elif option == 9:
quantity = int(input("Enter the quantity: "))
total = quantity * 120
print("The price is: " str(total))
elif option == 10:
quantity = int(input("Enter the quantity: "))
total = quantity * 200
print("The price is: " str(total))
total_cost = total
continue_order = int(input("Would you like another item? enter Yes--> (1) or--> No (0):"))
print("Your receipt:\n")
for option in cart :
print("Menu item number: ", option, "Quantity",quantity)
print("Total Price: ", total_cost)
例如,如果我選擇購買一個 7 個單位的漢堡和一個 3 個單位的比薩,則賬單顯示為漢堡,數量:3 比薩,數量:3 比薩的數量值覆寫了漢堡,我怎樣才能得到正確的對應值隨產品插入的數量
uj5u.com熱心網友回復:
請注意,我將價格存盤在串列中。如果出現這種情況,您也可以將專案名稱存盤在最后一個。
total_cost = 0
prices = [150, 120, 60, 180, 150, 90, 150, 120, 120, 200]
cart = []
while True:
option = int(input("Which item would you like to purchase?: "))
if option > len(prices):
print('we do not serve that here')
else:
quantity = int(input("Enter the quantity: "))
total = quantity * prices[option-1]
print("The price is:", total)
cart.append( (option, quantity) )
total_cost = total
if input("Would you like another item? enter Yes--> (1) or--> No (0):") == '0':
break
print("Your receipt:\n")
for option,quantity in cart :
print("Menu item number: ", option, "Quantity",quantity)
print("Total Price: ", total_cost)
uj5u.com熱心網友回復:
我會采取@tim-roberts 解決方案(我贊成)更進一步,并將我的選單項基于字典。這將允許您相當容易地添加描述等內容。
products = {
"1": {"id": "1", "cost": 150},
"2": {"id": "2", "cost": 120},
"3": {"id": "3", "cost": 60},
"4": {"id": "4", "cost": 180},
"5": {"id": "5", "cost": 150},
"6": {"id": "6", "cost": 90},
"7": {"id": "7", "cost": 150},
"8": {"id": "8", "cost": 120},
"9": {"id": "9", "cost": 120},
"10": {"id": "10", "cost": 200}
}
cart = []
while True:
product_id = input("Which item would you like to purchase?: ")
product = products.get(product_id)
if product:
quantity = int(input("Enter the quantity: "))
cart.append({
"id": product["id"],
"quantity": quantity,
"total": quantity * product["cost"]
})
print(f"\tYou requested {quantity} items at ${product['cost']} per item.")
else:
print('Sorry, we do not serve that here.')
continue_order = input("Would you like another item? Enter Yes--> (1) or--> No (0): ")
if continue_order == "0":
break
print("\nYour receipt:")
for product in cart :
print(f"\tMenu Item: {product['id']} Quantity: {product['quantity']} Total: {product['total']}")
cart_total = sum(p["total"] for p in cart)
print(f"\tTotal Price: ${cart_total}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415999.html
標籤:
