Exercises and Solutions

Every Find the Bug exercise in the book, free, with every worked solution — 445 of them, across 12 chapters. Read them here, or download all 890 programs as .py files and run them.

How to use them

Each exercise is a short, complete program that does something recognizable — totalling a quiz, splitting a bill, listing planets — and hides exactly one mistake. Nothing else about it is wrong.

The productive order is:

  1. Read it and predict. Say out loud what you expect the program to print. The comment on the last line usually tells you what it should print.
  2. Find the difference. If your prediction and the intended output disagree, the gap is the bug.
  3. Run it. Confirm you were right — or find out you were not, which is more useful.
  4. Then open the solution. Not before. The diagnosis is where the learning is, and reading it first spends the exercise for nothing.

Solutions stay folded behind a click for exactly that reason.

The three kinds of bug

Every solution names which kind it is. Learning to sort a failure into one of these three bins, quickly, is the first move of any real debugging session.

Kind What happens Example Here
Syntax Python cannot parse the file. Nothing runs at all. A missing : after if 60
Runtime It starts, then fails partway with an exception. count + "10" — you cannot add an int and a str 147
Logical It runs, finishes cleanly, and is wrong. students / teams where students // teams was meant 237

The third kind is the one beginners are never warned about, and the one that causes real damage — which is why it is deliberately the largest group here: 237 of the 445 exercises are logical bugs, more than syntax and runtime combined.

The three counts come to 444; the remaining exercise sits on the line between runtime and logical, and its solution names both.

By chapter

ChapterExercisesSolutionsCount
Ch. 5 — Data Structures Exercises Solutions 50
Ch. 6 — Objects Exercises Solutions 25
Ch. 7 — Operators Exercises Solutions 25
Ch. 8 — Input and Output Exercises Solutions 5
Ch. 9 — Control Flow Exercises Solutions 55
Ch. 10 — Functions Exercises Solutions 60
Ch. 11 — Scoping Exercises Solutions 30
Ch. 13 — Modules Exercises Solutions 10
Ch. 16 — Handling Failures Exercises Solutions 20
Ch. 17 — Testing Exercises Solutions 15
Ch. 18 — Bugs Exercises Solutions 20
Ch. 20 — Common Pitfalls Exercises Solutions 130

Running them on your own machine

The archive unpacks into two folders:

everyday-programming-exercises/
    exercises/ex_<chapter>_<group>_<n>.py    the program with the bug in it
    solutions/sol_<chapter>_<group>_<n>.py   the diagnosis and the fix

Almost every program runs on a stock Python 3 with nothing installed — the imports are all standard library (math, copy, random, statistics). Two sets need a little more:

  • Chapter 17 (testing) uses pytest: pip install pytest.
  • Section 13.2 (writing your own module) imports modules such as conversions and shapes that you are meant to have written. Those four exercises are about the import mechanism itself, so they raise ModuleNotFoundError until you create the module beside them — the chapter walks you through it.

Everything else just runs:

$ python3 exercises/ex_5_1_1.py
25

That 25 is the bug: the program was supposed to print 75.

Some exercises will not run at all, and that is the point — a SyntaxError or a traceback is the bug you were asked to find. Each file opens with a docstring saying what the program was supposed to do, so you always know what correct looks like.

For teachers. All 445 exercises and solutions are licensed under Apache 2.0, the same as the book. Use them in a class, a workshop, or a problem set. The per-chapter pages are stable URLs you can link to directly, and the numbering matches the printed book, so Exercise 9.4.2 means the same thing on paper and on screen.

A worked example

Exercise 5.2.1 — Average temperature. Three readings are 20.0, 22.0 and 24.0. The program should print their average, 22.0.

reading1 = 20.0
reading2 = 22.0
reading3 = 24.0

average = reading1 + reading2 + reading3 / 3
print(average)   # 22.0
Show the diagnosis and the fix

Bug type: Logical

Without parentheses, only reading3 / 3 is divided — operator precedence puts division ahead of addition — so the program prints 50.0. Wrap the addition in parentheses before dividing.

average = (reading1 + reading2 + reading3) / 3
print(average)   # 22.0

That is the shape of all 445 of them. Start with Chapter 5.

results matching ""

    No results matching ""