i made a shopping program and Im trying to obtain the value of the quantity inserted with the corresponding product but it only prints the last inputted value and overwrite all of the previous values
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)
eg if i chose to buy a burger of 7 units and a pizza of 3 units the bill comes out like burger , quantity: 3 pizza , quantity: 3 the pizza's quantity value overwrites the burgers , how can i get the correct corresponding value of the inserted quantity with the product
CodePudding user response:
Note that I'm storing the prices in a list. You could also store the item name in that last as well, if that comes up.
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)
CodePudding user response:
I would take @tim-roberts solution (which I upvoted) one step farther and base my menu items on a dictionary. This would allow you to add things like a description fairly easily.
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}")
