# 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 November 19, 2026

By Gregor Dzierzon

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](https://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:

```bash
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):

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

On a Mac (zsh):

```bash
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:

```bash
python seed.py
```

```text
Wrote classes.csv: 588 class sessions.
```

The folder holds:

- `seed.py`: makes `classes.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 for `center.py` and for the saved `story.ipynb`, and `conftest.py`, which gives every test its own fresh copy of the data. You don't need to change them.
- `requirements.txt` and `pytest.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:

```python
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.")
```

1. **`fill_rate_by_category(df)`** returns a new table with the columns `category`, `sessions` and `fill_rate`: each category's number of sessions, and its `signed_up` added up, divided by its `capacity` added 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.
2. **`busiest_month(df)`** returns the month, as text such as `'2026-01'`, whose sessions had the most people attend.
3. **`no_show_rate(df)`** returns the percent of sign-ups who didn't come, rounded to 1 decimal place, using only the sessions whose `attended` isn'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`.
4. **`small_groups(df, min_sessions)`** returns a list, in A to Z order, of the categories with fewer than `min_sessions` sessions. Twenty fitness sessions, three arts and one technology, with `min_sessions` 5, 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:

1. **A title.** Replace the first cell, the note, with a Markdown cell whose first line is a title written with one `#`.
2. **Four headings, in this order,** each a Markdown line that starts with `## `: `## The question`, `## The short answer`, `## The evidence` and `## 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 done` section too, as long as the four come in this order.
3. **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.
4. **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.
5. **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:

```bash
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:

```text
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:

```bash
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.

1. 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.
2. 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:

   ```bash
   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 main
   ```

3. Paste 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.

## Key terms

- [Notebook](https://www.ask-codey.com/glossary/notebook-python)
- [Markdown](https://www.ask-codey.com/glossary/markdown)
- [Restart and Run All](https://www.ask-codey.com/glossary/restart-and-run-all)
- [Execution Count](https://www.ask-codey.com/glossary/execution-count)
- [Sample Size](https://www.ask-codey.com/glossary/sample-size)
- [pytest](https://www.ask-codey.com/glossary/pytest)

## Related

- [A Notebook That Tells a Story](https://www.ask-codey.com/lessons/data-notebook-that-tells-a-story)
- [Tables People Can Read](https://www.ask-codey.com/lessons/data-tables-people-can-read)
- [What the Data Can and Can't Show](https://www.ask-codey.com/lessons/data-what-the-data-cant-show)
- [Sharing a Notebook as a Web Page](https://www.ask-codey.com/lessons/data-exporting-notebooks)

Source: https://www.ask-codey.com/lessons/exercise-data-community-center-story
