Home > OS >  Having problems computing total value
Having problems computing total value

Time:01-26

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():
  print(f"\t\t\t\t{'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=' ')
    print()


menu()

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")
        #fries

    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":
        cart.append(food[1])
        i = int(input("Enter quantity:"))
        total = total   20
        total = total * i
        fs = i * 20
        print(fs)
      elif size == "M" or size == "m":
        cart.append(food[1])
        i = int(input("Enter quantity:"))
        total = total   35
        total = total * i
        fm = i * 35
        print(fm)
      elif size == "l" or size == 'L':
        cart.append(food[1])
        i = int(input("Enter quantity:"))
        total = total   50
        total = total * i
        fl = i * 50
        print(fl)
      else:
        print("Invalid size")
      #cart.append(food[1])
      #cost.append(price[1])

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


ordering()


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

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

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


inside_cart()

CodePudding user response:

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!

  • Related