liveBook · Manning

Menu

As explained in A Pythonic Adventure: From Python basics to a working web app

A menu is a presented list of products or options that a customer can choose from.

A menu presents choices so a customer can place an order without guessing what is available. In a coffee-shop program, separate menus hold drinks, flavors, and toppings. Each menu is a Python list of quoted strings, making the available products easy to name, print, and reuse. section 2.0

drinks = ["chocolate", "coffee", "decaf"]
flavors = ["caramel", "vanilla", "peppermint", "raspberry", "plain"]
toppings = ["chocolate", "cinnamon", "caramel"]
 
print("Erik's Coffee Shop drinks")
print("-------------------------")
i = 1
for d in drinks:
    print(i, d)
    i = i + 1
 
print("Erik's Coffee Shop flavors")
print("--------------------------")
i = 1
for f in flavors:
    print(i, f)
    i = i + 1
 
print("Erik's Coffee Shop toppings")
print("---------------------------")
i = 1
for t in toppings:
    print(i, t)
    i = i + 1

This listing turns three lists into friendly numbered menus. A small counter starts at 1 for each category, while a for loop prints every available item. section 2.0

Printing a whole list is quick, but it exposes Python's bracket-and-quote representation. A loop produces a customer-friendly menu one item at a time. Reset the counter before each category when drinks, flavors, and toppings should each begin at 1. section 2.0

A choice menu needs a careful handoff from display to selection:

  • Display numbered choices and ask the customer to enter a selection with input(). section 2.0
  • Convert the entered value with int(), because input produces a string but list indexing requires an integer. section 2.0
  • Subtract 1 before indexing when displayed choices start at 1, since the first item in a Python list has index 0. section 2.0
  • Use the selected drink, flavor, and topping to print the completed order. section 2.0

Why choice 1 needs a minus one

A displayed menu may label chocolate as 1, coffee as 2, and decaf as 3. If the customer enters 1, drinks[int(drink)] selects the second list item. drinks[int(drink) - 1] selects chocolate instead. The visible numbering is for people; the list index is for Python. section 2.0

A main menu is the larger control loop around ordering. It repeatedly gets an order, shows it for confirmation, saves confirmed orders, and returns to the start for the next customer. Breaking those jobs into get_order(), print_order(), and save_order() keeps the overall algorithm readable before each detail is filled in. section 6.0

Listing 6.1 A main menu with simple functions
def main_menu():
    while True:
        order = get_order()
        print("Check your order:")
        print_order(order)
        confirm = input("Confirm? Press Y to confirm, N to cancel: ")
        if confirm == "Y" or confirm == "y":
            save_order(order)
            print("Thanks for your order:")
            print_order(order)
        else:
            continue
 
 
def get_order():
    return {}
 
 
def print_order(order):
    print(order)
    return
 
 
def save_order(order):
    print("Saving order...")
    return
 
main_menu()

This skeleton makes the main menu visible as a repeating workflow: collect an order, check it, confirm it, save it when approved, then serve the next customer. section 6.0

A simple main-menu cycle is:

  • Get an order, potentially collecting a customer's name, drink, flavor, and topping. section 6.0
  • Print the order and ask for Y or y to confirm, or cancel it with another response. section 6.0
  • Save and print a confirmed order; otherwise continue the loop and begin again. section 6.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Works through list creation, loop printing, input conversion, index adjustment, and a function-based main-menu skeleton.
Examples
Uses drinks, flavors, and toppings menus, a list-index TypeError, and a confirm-or-cancel ordering loop.
What it assumes
Teaches menu construction from Python lists while using strings, variables, loops, input(), and basic functions.
What you can do after
Build it from scratch by printing numbered product menus, retrieving selections, and looping through customer confirmations.
How it connects
Connects Python lists, for loops, input(), int(), list indices, dictionaries, functions, and while loops.

Also explained in

Compare how 3 books explain menu →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.