Exercise: The Bakery Menu Board

Finish Maple Street Bakery's menu board, a Python program that prints each item with print(), fixing a line with a missing quote, switching a sold-out item off with a comment and giving print() a number, then let pytest check every line and hand it in with Git.

Novice · Python · Updated

By

Maple Street Bakery writes its menu on a board by the door every morning, and the owner wants the same board printed by a program, so it can go on the screen by the counter. Your job is to finish that program: one print() line for each item. One line has a mistake in it, one item has sold out, and one line gives print() a number. Tests run your program and check every line it prints, so you'll know when the board is right. This is the course's first exercise, so this lesson walks through every step, from getting the code to handing it in.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-python-bakery-menu-board, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-python-bakery-menu-board.git

Git makes a folder, exercise-python-bakery-menu-board, with the exercise code inside. Get the Exercise Code with Git walks through cloning step by step. If you can't clone it, click Code on the repository's page, then Download ZIP, and unzip it into your python-course folder instead.

Open that folder in VS Code (File > Open Folder) and open its terminal with Terminal > New Terminal. The tests use pytest, which goes in a virtual environment: a private set of packages for this folder only. Make it with three commands. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The first makes the virtual environment, the second turns it on, so the prompt starts with (.venv), and the third installs pytest into it. Virtual Environments and pip, in Module 3, explains all three; every exercise starts with them. With the virtual environment on, plain python works on a Mac too. If PowerShell says running scripts is disabled, the exercise's README has the one-line fix.

The folder holds:

  • menu.py: the menu board program, for you to finish. The comment at its top shows every line it should print.
  • tests/test_menu.py: the tests, which run your program and read each line it prints. You don't need to change it.
  • requirements.txt and pytest.ini: the package the tests need, and where pytest finds the tests.

What to do

Each problem in menu.py is a comment with an empty line or two under it. An example at the top shows the format:

# Example i. The bakery's name.
print("i) Bakery: Maple Street Bakery")
i) Bakery: Maple Street Bakery

Every line you print has the same shape: the problem number, ), the item's name, : , then its price or value.

  1. Problems 1 to 4: print one line for each item, under its comment, such as 1) Blueberry muffin: $3.25.
  2. Problem 5: a baker started this line, and it has a mistake: Python can't run the file until it's fixed. Run the program, read the traceback, and fix the line.
  3. Problem 6: the lemon tart sold out. Turn its line into a comment, so it stays in the file for tomorrow but doesn't print today.
  4. Problem 7: print 7) Loaves left: 14, giving print() two values: the text in quotes, a comma, then the number 14 with no quotes.
  5. Problem 8: print the opening hours, 8) Open: 7 AM to 2 PM.

Check your work

Run the program from the exercise folder:

python menu.py

The first time, Python prints a traceback ending in a SyntaxError, and nothing else: that's problem 5 at work. Then check every line with the tests:

pytest

At first all 9 tests fail. That's expected: each problem you finish turns its test green. While menu.py stops with an error, every test fails with the end of that traceback: fix the error first, and the rest start to pass one by one. A failing test then says the line it expected, for example menu.py printed no line 1. It should print: 1) Blueberry muffin: $3.25.

Hints

  • Copy the pattern. Every line you write is shaped like the example, with different words.
  • Read the traceback from its last line up. The last line names the error and says what's wrong, and the File line above it gives the line number. Your First Python Program reads one with you.
  • A hash switches a line off. # at the start of a line turns it into a comment. In VS Code, Ctrl+/ (Cmd+/ on a Mac) does it for you.
  • print() takes several values. Separated by commas, they print on one line with a space between each: print("Loaves left:", 14) prints Loaves left: 14.
  • Run it often. Run python menu.py after every line or two, so a mistake is never far from where you're working.

When you're done

Every test passes: pytest's last line says 9 passed. And python menu.py prints the whole board:

i) Bakery: Maple Street Bakery
1) Blueberry muffin: $3.25
2) Cranberry scone: $3.50
3) Sourdough loaf: $6.50
4) Cinnamon roll: $3.75
5) Bread of the day: Rye
7) Loaves left: 14
8) Open: 7 AM to 2 PM

The lemon tart is back tomorrow. Which one change brings it back, and what else would you change if its price went up to $4.75?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-python-bakery-menu-board), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. If you cloned the starter, point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-python-bakery-menu-board.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    

    If you downloaded the ZIP instead:

    git init -b main
    git add .
    git commit -m "My solution"
    git remote add origin https://github.com/your-name/exercise-python-bakery-menu-board.git
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-python-bakery-menu-board, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

You cloned a project, ran it, read a real traceback and fixed the line it pointed to, let tests tell you when the work was right, and handed it in with Git. Every exercise in this course follows that same loop, and so does a developer's working day.

Key terms

Related