Exercise: Bean Street's First Notebook
Fix a coffee chain's first notebook, whose cells are in the wrong order and which has no title, by adding a Markdown title, putting the cells in an order that runs from top to bottom, running Restart and Run All and saving, then let pytest read the saved notebook and hand it in with Git.
Novice · Python · Updated
Rosa, who owns Bean Street Coffee, asked for a notebook that adds up her Campus store's first 12 orders of the morning. Someone started it and saved it, and it doesn't work for anyone else: its cells are in the wrong order, so one of them uses a name that a cell further down makes, and it has no title. Your job is to fix it so it runs from top to bottom for anyone who opens it. A notebook is saved as JSON, so tests can read your saved notebook and check its title, the order its cells ran in, and what the last cell printed. 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-data-first-notebook, 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-data-first-notebook.git
Git makes a folder, exercise-data-first-notebook, 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), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. 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 command makes the virtual environment, the second turns it on, so the prompt starts with (.venv), and the third installs pandas, ipykernel and pytest into it. Virtual Environments and pip explains all three, and Setup: pandas and Notebook Support shows what each package is for; every exercise in this course starts with them. If PowerShell says running scripts is disabled, the exercise's README has the one-line fix.
The folder holds:
first_look.ipynb: the notebook, for you to fix. The morning's 12 orders are in one of its cells, so there's no data file.tests/test_notebook.py: the tests, which read your saved notebook. You don't need to change it.requirements.txtandpytest.ini: the packages the exercise needs, at exact versions, and where pytest finds the tests.
Open first_look.ipynb, click Select Kernel (top right), choose Python Environments, then .venv.
What to do
The notebook has five code cells. In the order they're saved, they hold: a loop that adds up the prices, a line that prints how many orders there are, the 12 orders, a cell that prints pandas' version, and the line that prints the total, print(f"1) total: ${total:.2f}").
- Add a title. Add a Markdown cell at the very top whose first line is a title written with one
#, such as# Bean Street's First Notebook. A sentence under it saying what the notebook does is a good habit. - Put the cells in a working order. Move each code cell below the cell that makes the names it uses. The cell that prints
1) total: $…stays last. Move the cells; don't change the code inside them. - Run it from a fresh start. Click Restart, then Run All, in the notebook's toolbar. Every cell runs without an error, and the execution counts read 1, 2, 3, 4, 5 from top to bottom.
- Save with Ctrl+S (Cmd+S on a Mac). The tests read the saved file.
Check your work
Run the tests from the exercise folder, with the virtual environment on:
python -m pytest
At first most of the 8 tests fail. That's expected: the notebook has no title and hasn't run. Each step you finish turns its tests green. A failing test says what it expected and what it found, for example The execution counts, from top to bottom should be [1, 2, 3, 4, 5], but it is [None, None, None, None, None]. None means a cell hasn't run.
The tests read the notebook as you last saved it, so after every change: Restart and Run All, save, then run the tests again.
Hints
- Let the errors guide you. Run the cells one at a time from the top. A
NameErrornames the variable a cell needs; find the cell that makes it and move it above, as The Kernel: Why Run Order Matters shows. - Moving cells. Drag a cell by the handle at its left edge, or click it and press Alt+Up or Alt+Down.
- Adding a cell above the first. Hover over the first cell's top edge and click + Markdown, as Your First Notebook in VS Code does.
- One cell proves the kernel. If the pandas cell stops with
ModuleNotFoundError, the notebook isn't using.venv: choose it with Select Kernel, then Restart and Run All.
When you're done
Every test is green: pytest's last line says 8 passed. And the last cell of your notebook shows:
1) total: $47.50
The pandas cell could sit in several places and the notebook would still run. Which places, and why can't the cell with the 12 orders move as freely?
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.
On GitHub, click +, then New repository. Name it (for example
exercise-data-first-notebook), choose Public, and click Create repository. Leave out the README: your folder already has one.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-data-first-notebook.git git add . git commit -m "My solution" git push -u origin mainIf 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-data-first-notebook.git git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-data-first-notebook, 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
A notebook that runs from top to bottom after a fresh start is one anyone can trust and rerun. Restart and Run All, then save, is the habit behind every notebook you'll hand in or share.