Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106041

Re: Calculate Bill

From BartC <bc@freeuk.com>
Newsgroups comp.lang.python
Subject Re: Calculate Bill
Date 2016-03-30 11:19 +0100
Organization A noiseless patient Spider
Message-ID <ndg914$m0d$1@dont-email.me> (permalink)
References <ba29ab8f-1076-4a00-8b2e-b4bbe0fbae60@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Calculate Bill Yum Di <nevinadias3@gmail.com> - 2016-03-29 15:33 -0700
  Re: Calculate Bill Chris Angelico <rosuav@gmail.com> - 2016-03-30 10:34 +1100
  Re: Calculate Bill Steven D'Aprano <steve@pearwood.info> - 2016-03-30 11:50 +1100
  Re: Calculate Bill BartC <bc@freeuk.com> - 2016-03-30 11:19 +0100

csiph-web