Exercise: Community Center Story
Write four tested functions that find which classes fill up, the busiest month, the no-show rate and the categories too small to trust at a community center, then tell the story in a notebook under four set headings, run it from a fresh start, and let pytest read the saved notebook.
Journeyman · Python · Updated
Denise runs Pine Valley Community Center, and she's planning next year's classes. She wants to know which kinds of classes fill their seats, which month is busiest, and how many people who sign up don't come, and she doesn't read Python. You'll work in two parts: four functions in center.py that find the answers, checked by tests, then a story notebook that tells Denise what they mean, with its evidence and what the data can't tell her. A notebook is saved as JSON, so tests can read your saved story too: its title, its headings, what's under them, and the order its cells ran in.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-data-community-center-story, 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-community-center-story.git
Git makes a folder, exercise-data-community-center-story, with the exercise code inside. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder. 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 prompt starts with (.venv) once the virtual environment is on. This exercise's requirements.txt adds nbconvert to the usual packages, for sharing the story as a web page at the end. Then make the data, once:
python seed.py
Wrote classes.csv: 588 class sessions.
The folder holds:
seed.py: makesclasses.csv, every class session the center ran from November 2025 to October 2026, our own made-up data. Its fixed random seed means everyone gets the same rows.center.py: the four functions, for you to finish.story.ipynb: the story notebook, for you to write. It starts with a note about what goes in it and a cell that loads the data and imports your functions.explore.ipynb: a notebook for looking at the data and trying your code. The tests don't read it.report.py: prints what your four functions find. Ready to use.tests/: the tests forcenter.pyand for the savedstory.ipynb, andconftest.py, which gives every test its own fresh copy of the data. You don't need to change them.requirements.txtandpytest.ini: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.
Open each notebook and choose the .venv kernel with Select Kernel (top right).
What to do
Part 1: the functions. Every function in center.py is declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should do, with an example:
def busiest_month(df):
"""Return the month whose sessions had the most people attend, as text such as '2026-01'.
Add up attended for each month and return the month with the largest total. (A blank
attended, where the teacher didn't record who came, adds nothing.)
Two sessions in '2026-01' (12 and 9 came) and one in '2026-02' (15 came): '2026-01',
because 21 is more than 15.
"""
raise NotImplementedError("Not built yet.")
fill_rate_by_category(df)returns a new table with the columnscategory,sessionsandfill_rate: each category's number of sessions, and itssigned_upadded up, divided by itscapacityadded up, times 100, rounded to 1 decimal place. The highest fill rate comes first, and the rows are numbered from 0. Two arts sessions with 10 seats (8 and 6 signed up) and one kids session with 10 seats (9 signed up) give kids, 1 session, 90.0, then arts, 2 sessions, 70.0.busiest_month(df)returns the month, as text such as'2026-01', whose sessions had the most people attend.no_show_rate(df)returns the percent of sign-ups who didn't come, rounded to 1 decimal place, using only the sessions whoseattendedisn't missing. Sessions of 10 signed up and 8 came, and 10 signed up and 9 came, give 3 no-shows in 20 sign-ups:15.0.small_groups(df, min_sessions)returns a list, in A to Z order, of the categories with fewer thanmin_sessionssessions. Twenty fitness sessions, three arts and one technology, withmin_sessions5, give['arts', 'technology'].
Each function leaves the table it was given unchanged; the tests check that too.
Part 2: the story. Open story.ipynb and write the story for Denise:
- A title. Replace the first cell, the note, with a Markdown cell whose first line is a title written with one
#. - Four headings, in this order, each a Markdown line that starts with
##:## The question,## The short answer,## The evidenceand## What this data can't tell us. Under the question and the short answer, write at least a sentence of 5 words or more each. Under the evidence, at least one code cell that shows a result. Under what the data can't tell us, a list of at least 2 points, each a line that starts with-. You can add a## How it was donesection too, as long as the four come in this order. - Use your functions. Between them, the code cells call all four functions from
center.py. Keep the cell that loads the data and imports them near the top, under the title. - Run it from a fresh start: Restart, then Run All. Every cell runs without an error, and the execution counts read 1, 2, 3 and on, from the top.
- 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 all 21 tests fail. That's expected: each function you finish, and each part of the story you write, turns its tests green. A failing test says what it expected and what it found, for example busiest_month(classes) should give '2026-01', but it gave '2026-07'.
The story's tests read the notebook as you last saved it, so after every change: Restart and Run All, save, then run the tests again. To see your functions at work on the whole year, run python report.py.
Hints
- Write the functions first, the story last. The short answer quotes numbers the evidence cells show, so it's easiest to write once they run. A Notebook That Tells a Story shows the shape.
- A fill rate is two sums divided, not an average of each session's rate. Named aggregation,
groupby("category", as_index=False).agg(...), gives the count and both sums in one step. - The busiest month is a label, not a number.
idxmax()gives the label of a Series' largest value. - Leave out what wasn't recorded.
dropna(subset=["attended"])keeps the sessions with an attendance, as What the Data Can and Can't Show explains. - Restart before you trust the notebook. After you change
center.py, the kernel still holds the old functions until you restart it.
When you're done
Every test is green: pytest's last line says 21 passed. And python report.py prints:
1) sessions: 588
2) fullest category: technology 93.3
3) sessions in the fullest category: 18
4) kids fill rate (%): 86.5
5) busiest month: 2026-01
6) sessions with no attendance recorded: 12
7) no-show rate (%): 11.6
8) categories with fewer than 30 sessions: technology
Then share the story as a web page, without the code, as Sharing a Notebook as a Web Page shows:
jupyter nbconvert --to html --no-input story.ipynb
Open story.html and read it as Denise would. Lines 2 and 3 say technology classes are the fullest, from 18 sessions. Would you put that first in Denise's short answer, and what would you say about it?
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-community-center-story), 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. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-data-community-center-story.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-data-community-center-story, 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
The numbers are only half the work; the story is what Denise can act on. Tested functions make every number trustworthy, and a notebook that runs from the top, with the answer first and its limits named, makes it something she can read.