obtaining all the previous values of a variable
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
Solution 1:
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)