Search code examples
pythonlistdictionaryordereddictionary

Creating a restaurant/pizza menu in python, with a good layout


I am new to python and would like to create a Pizza ordering system using python, that looks something like this.

1   Hawaiian                      $7.50
2   Champagne Ham & Cheese        $7.50
3   Beef & Onion                  $7.50
4   Pepperoni                     $7.50
5   Simply Cheese                 $7.50
6   Bacon & Mushroom              $7.50
7   Italiano                      $7.50
8   The Deluxe                    $13.50  
9   Ham, Egg & Hollandaise        $13.50
10  Americano                     $13.50
11  Mr Wedge                      $13.50
12  BBQ Meatlovers                $13.50

I would then like the customer to be able to select different pizzas based on the number so that they don't have to type Hawaiian, they can instead type 1. I have tried both dictionaries and lists but always resulted in this problem:

1  Hawaiian $7.50
2  Champagne Ham & Cheese$7.50
3  Beef & Onion $7.50
4  Pepperoni $7.50
5  Simply Cheese $7.50
6  Bacon & Mushroom $7.50
7  Italiano $7.50
8  The Deluxe $13.50 
9  Ham, Egg & Hollandaise $13.50
10  Americano $13.50
11  Mr Wedge $13.50 
12  BBQ Meatlovers $13.50

Where the prices would "stick to the pizza name" and the names of pizzas 10 onwards were indented by one character. Manually putting spaces does not seem like the most efficient way of going about my problem. e.g

standardprice = "$7.50"
    deluxeprice = "13.50"
    print (
    """   
1   Hawaiian                      %s
2   Champagne Ham & Cheese        %s
3   Beef & Onion                  %s
4   Pepperoni                     %s
5   Simply Cheese                 %s
6   Bacon & Mushroom              %s
7   Italiano                      %s
8   The Deluxe                    %s  
9   Ham, Egg & Hollandaise        %s
10  Americano                     %s
11  Mr Wedge                      %s
12  BBQ Meatlovers                %s
     """  % (standardprice, standardprice, standardprice, standardprice, 
             standardprice, standardprice, standardprice,deluxeprice,            
             deluxeprice,deluxeprice, deluxeprice, deluxeprice)
        )

Is there an easier way to solve my problem? Also as a side question when printing is there a way that "standardprice" variable applies to the first 7 elements and deluxe price applies to 8-12, rather than the crude way I have done it.

here had a somewhat similar problem but the page did not help.Code Academies "A day at the supermarket tutorial" was not particularly helpful either. I'm very new to python, so explaining like I know nothing would be very helpful


Solution

  • There are some super handy utilities for string formatting: ljust|rjust, for just this sort of thing.

    Here's a quick example I whipped up using ljust, but don't stop here; take a peek at the doc linked above and let your imagination go wild with string-formatting-freedom!

    from collections import namedtuple
    
    MenuEntry = namedtuple('MenuEntry', ['index','description','price'])
    _menu = []
    _menu.append(MenuEntry(1, 'Hawaiian', '$7.50'))
    _menu.append(MenuEntry(2, 'Champagne Ham & Cheese', '$7.50'))
    _menu.append(MenuEntry(3, 'Beef & Onion', '$7.50'))
    _menu.append(MenuEntry(40, 'Pepperoni', '$10.50'))
    _menu.append(MenuEntry(100, 'Simply Cheese', '$17.50'))
    
    for entry in _menu:
        index = str(getattr(entry,'index')).ljust(5)
        descr = getattr(entry,'description').ljust(25)
        price = getattr(entry,'price').ljust(7)
        print '{0}{1}{2}'.format(index,descr,price)
    
    """ Output: """
    
    1    Hawaiian                 $7.50  
    2    Champagne Ham & Cheese   $7.50  
    3    Beef & Onion             $7.50  
    40   Pepperoni                $10.50 
    100  Simply Cheese            $17.50 
    
    """"""""""""""""""""""""""""