
Patrick B. answered 04/24/21
Math and computer tutor/teacher
The best way is to create a class to store the name ,price, and quantity ordered.
Next create an array of these objects which can then be displayed in the menu, sub totaled, and receipt created.
The other option is to create the dictionary for the name and price. You then need an array to store the quantity ordered.
Keeping 3 arrays is bad idea.
############# HERE YA GO !!!! .......................................
class Item:
def __init__(self, name, price):
self.name = name
self.price = price
self.qty = 0
def GetName(self):
return (self.name)
def GetPrice(self):
return(self.price)
def GetQty(self):
return(self.qty)
def SetQty(self,qty):
self.qty = qty
def Subtotal(self):
return( float(self.price)*int(self.qty))
###################################################
def ShowItems(items):
N = len(items)
sum=0
print(" Item # Item Price Qty Subtotal")
for i in range(N):
subtotal = items[i].Subtotal()
sum = float(sum) + float(subtotal)
print( str(i+1) + " " +
items[i].GetName() + " "+
str(items[i].GetPrice()) + " "+
str(items[i].GetQty()) + " " +
str(subtotal))
return(sum)
############################################################
def AskDone():
done = -1
while (int(done)!=0) and (int(done)!=1):
done = input(" Will this complete the order ??? 1=Yes 0=No :>")
return(done)
################################################################
def UpdateItems(items):
itemNum=-1
numitems = len(items)
itemQty=-1
while (int(itemNum)<1) or (int(itemNum)>int(numitems)):
itemNum = input("Please input the item # :>")
while (int(itemQty)<1):
itemQty = input("How many " + str(items[int(itemNum)-1].GetName()) +":>")
items[int(itemNum)-1].SetQty(int(itemQty))
################################################################
BlueberryMuffin = Item("Blueberry Muffin",1.25)
ChocolateMuffin = Item("Chocolate Muffin",1.50)
ChocolateChipCookie = Item("Chocolate Chip Cookie",0.50)
RainbowSprinkleCookie = Item("Rainbow Sprinkle Cookie",0.50)
Coffee = Item("Coffee",3.50)
Croissant = Item("Croissant",1.00)
items = []
items.append(BlueberryMuffin)
items.append(ChocolateMuffin)
items.append(ChocolateChipCookie)
items.append(RainbowSprinkleCookie)
items.append(Coffee)
items.append(Croissant)
done=-1
while (int(done)!=1):
ShowItems(items)
UpdateItems(items)
done = AskDone()
print("******************************")
totalSum = ShowItems(items)
print("total is : " + str(totalSum))
John D.
Thanks for the suggestion!04/24/21