Calculate Bill

BartC bc at freeuk.com
Wed Mar 30 06:19:12 EDT 2016


On 29/03/2016 23:33, Yum Di wrote:

> print ("Menu")
>
> print (
>      "1 = cheese and tomato: 3.50, "
>      "2 = ham and pineapple: 4.20, "
>      "3 = vegetarian: 5.20, "
>      "4 = meat feast: 5.80, "
>      "5 = seafood: 5.60 " )

> Hey.. this code works.

Sure, after you got rid of those list that were causing the trouble!

  However, i need it to calculate the total cost.
> I dont know how to do that. Can someone help me..
> thanks

But I think you will need a list of some sort as central place to store 
descriptions and prices. Several lists actually for the different menus. 
There are a dozen ways to this. One simple approach is below.

pizzas=( ("Cheese and Tomato", 3.50),  #0
          ("Ham and Pineapple", 4.20,), #1
          ("Vegetarian",5.20),          #2
          ("Meat Feast",5.80),          #3
          ("Seafood",5.60))             #4

descr = 0        # indices into each record
cost  = 1

def showmenu(menu):
     for number,selection in enumerate(menu,1):
         print ("{:>3} {:30} {:3.2f}".format(number,
                selection[descr],selection[cost]))

showmenu(pizzas)

option   = 3-1        # vegetarian (3 on displayed menu is 2 in list)
quantity = 2

print ("You chose",quantity,"of",pizzas[option][descr])
print ("Total is",pizzas[option][cost]*quantity)

-- 
Bartc



More information about the Python-list mailing list