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

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

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

Heres a quick example I whipped up using ljust, but dont 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 


Heres a working example, but I used the pandas library to save myself lots of grief. Go through the comments first if you can/want, then hit back if you have other questions.

Admittedly, this is not for beginners, but except for the formatting part for the DataFrame, its pretty basic.

import pandas as pd

pizzas = [
    Hawaiian,
    Champagne Ham & Cheese,
    Beef & Onion,
    Pepperoni,
    Simply Cheese,
    Bacon & Mushroom,
    Italiano,
    The Deluxe,
    Ham, Egg & Hollandaise,
    Americano,
    Mr Wedge,
    BBQ Meatlovers
    ]

df = pd.DataFrame(pizzas, columns=[Pizzas])
df.loc[:8, Prices] = 7.50     # First 7 items.
df.loc[8:, Prices] = 13.50    # All the rest.
df.index += 1                   # So that its not zero-indexed.
total_bill = 0.0                # Meh.

print Welcome to Pizza Planet! # So unoriginal, I know.
print
print Heres our menu!
print
# Following method taken and modified from unutbus amazing answer
# here: http://stackoverflow.com/questions/25777037
print df.to_string(justify=left,
                   header=False,
                   formatters={
                    Pizzas:{{:<{}s}}.format(
                        df[Pizzas].str.len().max()
                        ).format,
                    Prices:     ${:.2f}.format})
print
print Input a number and press enter to select an item.
print Input done to finish your order and tabulate your bill.
print Input exit to cancel your orders.

while True:

    order = raw_input(>>>  )

    if order == exit:
        break
    elif order == done:
        print Your total bill is ${:.2f}..format(total_bill)
        raw_input(Press any key to exit.)
        break
    elif int(order) in df.index:
        item = df.loc[int(order), Pizzas]     # Get the respective items
        price = df.loc[int(order), Prices]    # by indexing order input.
        print Youve selected {}! That would be ${:.2f}..format(item, price)
        total_bill += price
        continue
    else:
        print Dont be an idiot. # :-)
        raw_input(Press any key to exit.)
        break

Result:

enter

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

menu = int(input(   ORDER PLEASE n 1.kanji with n 2. fish with n 3.rise with chicken n n   Select Yours))

if menu == 1:
    print(n kanji with $10 )

    conform =int(input(n conform to press one))

    if conform ==1:
        print(n Order confirmed)
    else:
        print(chose correct option)

elif menu == 2:
    print(n fwith booti $15)

    confm = int(input(nconform to press one))

    if confm == 1:
        print(n Order confirmed)
    else:
        print(chose correct option)

elif menu == 3:
    print(npoola with booti $20)

    conform = int(input(n conform to press one))

    if conform == 1:
        print(n Order confirmed)
    else:
        print(chose correct option)

else:
    print(n WRONG SELECTION)

Leave a Reply

Your email address will not be published.