Having problems computing total value


im making a food ordering system but im having trouble with multiple order of the same item but in different sizes. I have put "fs,fm,fl" with a value of 0 so that it would be my basis as a counter for each sizes of my fries, i also have used "total" to add up every value of each sizes. After i put "2" large fries and "2" medium fries, My "total" and "fs+fm,+fl" is different from each other.

i expected my total and the added value to be the same but they are different. Is there an error with my computation? Is there any alternative/faster way to calculate the total value of different sizes? here is my code (it is unfinished)

food = ["Burger", "Fries", "Hotdog", "Sundae", "Coke Float"]
cart = []
cost = []
total = 0
fs = 0
fm = 0
fl = 0

def menu():
  collection = [["\nCode", "  Description", "     Size", "   Price"],
                ["  2", "     Fries", "         S/M/L", "     20/35/50"]]

  for a in collection:
    for b in a:
      print(b, end=' ')


first = True
def ordering():
  print("\nPlease select the CODE of the food you want to order")
  while first == True:

    global total
    global fs
    global fm
    global fl

    order = input("Type the code you want to order: \n")

    if order == ("2"):
      print("You have selected \n")
      print("\n", food[1], "\n")
      size = input("What size do you want? S/M/L: ")
      if size == "s" or size == "S":
        i = int(input("Enter quantity:"))
        total = total + 20
        total = total * i
        fs = i * 20
      elif size == "M" or size == "m":
        i = int(input("Enter quantity:"))
        total = total + 35
        total = total * i
        fm = i * 35
      elif size == "l" or size == 'L':
        i = int(input("Enter quantity:"))
        total = total + 50
        total = total * i
        fl = i * 50
        print("Invalid size")

      try_again = input("\nWould you like to add more? (Y/N):").lower()
      if try_again != ("y"):
      print("\nInvalid input. Please put only the CODE of the item\n")


def inside_cart():
  while first == True:
    print(' ')
    print(' ')
    print("The inside of your cart are currently:")
    print(fs + fm + fl,total)

    suggestion = input("would you like to order more? Y/N: ").lower()
    if suggestion.lower() == "y":

      print("\nOkay, proceeding to payment....\n")


>Solution :

I think the error originates in the price calculations:

total = total + 20
total = total * i
fs = i * 20

If you order two small fries this works fine the first time, as total becomes 20 and then 40 to account for it being two items. However, once you order the large fries you first add 50 for a total price of 90, and then multiply it by two for a price of 180, which should be 140.

To fix this, just change the total calculation to this:

total += i * price

Now you will add to the total only the price of the items ordered. As a rule of thumb, you should probably not multiply an accumulated value unless you’re sure that’s what needs to be done.

For the fs/fm/fl there seems to be an issue present too, as doing multiple orders for the same size will make the value stored in the variable only count the price of the last order of this size. If you for instance ordered small fries and then 1 small fries, fs would only be 20 when it should be 100. To fix that, you can apply an analogous change:

fs += i * 20

So make sure next time that each calculation is done separately, and try to imagine when some orders have already been done, how it would affect the values you are trying to calculate next.

On a different note, you may want to work on creating a table of sorts to store the different prices for items and sizes, as this code will quickly grow out of control for more items and be a nightmare to maintain.

Hope this helps!

Leave a Reply Cancel reply