# Exercise: Snack Bar Receipt

Finish a script that asks a cinema snack bar's customer for their order and prints the receipt, turning typed answers into numbers and showing the subtotal, the 7% tax and the total to the cent, while pytest types the answers in and checks every line.

Novice · Python · Updated November 19, 2026

By Gregor Dzierzon

The snack bar at Starlight Cinema has a program that asks each customer for their order, and stops there. You'll finish it: turn the typed answers into numbers, work out the subtotal, the 7% sales tax and the total, and print the receipt with every amount to the cent. It uses everything in this chapter: variables, math, strings, f-strings and `input()`. The tests run your script the way a customer would, typing the answers in for you, and check each line it prints.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-python-snack-bar-receipt](https://github.com/ask-codey/exercise-python-snack-bar-receipt), 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-python-snack-bar-receipt.git
```

Git makes a folder, `exercise-python-snack-bar-receipt`, 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, the same three steps as in the exercise before. 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. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.

The folder holds:

- `receipt.py`: the prices, the tax rate and the three questions. For you to finish.
- `tests/test_receipt.py`: the tests, which run `receipt.py`, type the answers in, and check the lines 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

`receipt.py` starts with three variables, `popcorn_price = 6.50`, `soda_price = 3.75` and `tax_rate = 0.07`, then asks its three questions with `input()`: the name for the order, how many popcorns and how many sodas. It prints an empty line, and the rest is yours. Below the comment that says *Your code goes here*:

1. **Tidy the answers.** Take the spaces off both ends of the name and give it a capital first letter, so `"  maya  "` becomes `"Maya"`. Turn the popcorns and the sodas into whole numbers.
2. **Work out the money.** The subtotal is the popcorns times the popcorn price plus the sodas times the soda price. The tax is the subtotal times the tax rate, rounded to cents. The total is the subtotal plus the tax.
3. **Print the receipt** as six numbered lines, each with the line's number, a closing parenthesis, its name and its value. Typing `Maya`, `3` and `2`, the receipt is:

```text
1) name: Maya
2) popcorns: 3
3) sodas: 2
4) subtotal: 27.00
5) tax: 1.89
6) total: 28.89
```

The subtotal, the tax and the total always show two decimal places, even when the last one is a zero. The README's *The rules* lists every order the tests type and every line they check.

## Check your work

Run the tests from the exercise folder, with the virtual environment on:

```bash
pytest
```

At first all 12 tests fail. That's expected: the script doesn't print a receipt yet, and each line you get right turns its tests green. A failing test's message says what it typed, the line it expected and what came back, for example:

```text
Typing 'Maya', '3', '2', receipt.py should print the line: 4) subtotal: 27.00
but it printed: 4) subtotal: 27.0
```

To try it the way a customer would, run `python receipt.py` and type the answers yourself.

## Hints

- **Work one order through by hand first.** 3 popcorns at $6.50 make 19.50, and 2 sodas at $3.75 make 7.50, so the subtotal is 27.00. 7% of 27.00 is 1.89, and the total is 28.89.
- **The answers arrive as text.** `input()` always gives back a string, so `"3"` has to become `3` before any math. A `TypeError` about multiplying a sequence means a number is still text. *Asking for Input, and Converting Types* shows `int()`.
- **Keep what a method gives back.** `name.strip().title()` makes a tidy copy; assign it to `name` to keep it. *Strings and f-strings* shows both methods.
- **Round once, then add.** `round(number, 2)` rounds the tax to cents. *Numbers and Math* shows why the unrounded tax has a long tail.
- **Let the f-string show the cents.** `{subtotal:.2f}` shows `27.00` where `{subtotal}` shows `27.0`.

## When you're done

Every test is green: pytest's last line says `12 passed`. And running `python receipt.py`, typing `Maya`, `3` and `2`, shows:

```text
Name for the order: Maya
How many popcorns? 3
How many sodas? 2

1) name: Maya
2) popcorns: 3
3) sodas: 2
4) subtotal: 27.00
5) tax: 1.89
6) total: 28.89
```

Now try typing `ten` for the popcorns. The program stops with a `ValueError`, as it would for any customer who types a word. *Handling Errors with try and except*, in Module 3, shows how a program catches that and asks again.

## 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-snack-bar-receipt`), 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-python-snack-bar-receipt.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-python-snack-bar-receipt`, 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

Ask, convert, calculate, print: that's the shape of most small programs, and a receipt that's right to the cent is proof you can put the four together.

## Key terms

- [input()](https://www.ask-codey.com/glossary/input)
- [Type Conversion](https://www.ask-codey.com/glossary/type-conversion)
- [round()](https://www.ask-codey.com/glossary/round)
- [f-string](https://www.ask-codey.com/glossary/f-string)
- [pytest](https://www.ask-codey.com/glossary/pytest)

## Related

- [Variables: Giving a Value a Name](https://www.ask-codey.com/lessons/python-variables)
- [Numbers and Math](https://www.ask-codey.com/lessons/python-numbers-and-math)
- [Strings and f-strings](https://www.ask-codey.com/lessons/python-strings-and-f-strings)
- [Asking for Input, and Converting Types](https://www.ask-codey.com/lessons/python-input-and-converting-types)

Source: https://www.ask-codey.com/lessons/exercise-python-snack-bar-receipt
