Python Quest

Track 1 · Lesson 10 of 11

Debugging: first steps

Find bugs in programs that run but give wrong answers: reproduce, trace, print values, and change one thing at a time.

About 30 minBeginner3 exercisesFree

You will be able to

  • Reproduce a bug with a specific input and a known correct answer
  • Trace a program by hand, line by line, writing down each variable's value
  • Add temporary print() calls to see what values a program really has
  • Fix bugs by changing one thing at a time and re-running

A crash with a traceback is the friendly kind of bug: Python tells you where to look. The sneakier kind is a program that runs perfectly and prints a wrong answer. A mistake in what the program does, rather than in how it is written, is called a logic error. Any mistake that makes a program misbehave, whether it crashes or prints a wrong answer, is called a bug, and finding and fixing bugs is called debugging.

Debugging isn't guesswork. It's a routine you can follow every time, and this lesson teaches its first steps.

Step 1: Reproduce the bug with a known answer

Choose an input and work out the correct output by hand. Now you can run the program with that input and see the difference clearly. "The total is wrong" is vague; "for 2 items at 50 SAR it prints 115.00, but the answer should be 100.00" is something you can investigate.

Step 2: Read the code aloud and trace it

Go through the program line by line, as Python would, and write down the value of each variable after each line. This is called tracing, and a table of the values is a trace table. Read each line aloud in plain words ("discount is price times fifteen, divided by a hundred"). Many bugs jump out the moment you say what the code really does rather than what you meant.

A program with a logic bug
price = float(input("Price: "))
discount = 15 / 100
new_price = price - discount
print(f"Price after 15% discount: {new_price:.2f} SAR")
Input given to the program: "200"

Try it: Work out the right answer for 200 SAR by hand first. Then run it and compare.

A 15% discount on 200 SAR should give 170.00, but the program prints 199.85. Here is its trace table for an input of 200:

LineCodeValues afterwards
1price = float(input(...))price = 200.0
2discount = 15 / 100discount = 0.15
3new_price = price - discountnew_price = 199.85

Line 3 subtracts 0.15 riyals, not 15 percent. Read aloud, it says "the new price is the price minus zero point one five", and the bug stands out. The fix is new_price = price - price * discount.

Step 3: Print the values you're unsure about

When a program is too long to trace comfortably in your head, ask it what's going on. Add temporary print() calls that show the values of variables at key points. Give each one a label so you know which line printed what. This is called print debugging, and professional programmers use it every day.

Temporary debug prints reveal the problem
quantity = input("Quantity: ")
unit_price = 12
print("DEBUG quantity:", quantity, type(quantity))
total = quantity * unit_price
print("DEBUG total:", total)
print("Total:", total)
Input given to the program: "3"

Try it: Run it and read the DEBUG lines. Then fix the bug, and delete both DEBUG lines once the total is right.

type(value) tells you what kind of value something is. Here it shows <class 'str'>: the quantity is still text, so * repeats it (333333333333) instead of multiplying. You'll meet type() properly in the next track; for now it's a handy debugging tool.

Remove the debug prints when you're done. They are scaffolding, not part of the program, and the exercise checker compares all of your output, so a leftover DEBUG line makes a test fail.

Step 4: Change one thing at a time

When you have an idea for a fix, make one change and run the program again with your test input. If it fixed the bug, good. If it didn't, undo it before you try something else. Changing several things at once makes it impossible to tell which change helped, and it becomes likely that you introduce a new bug while fixing the old one.

Finally, test with one or two more inputs, including an edge case such as 0. A fix that works for one input might still be wrong for others.

Predict the output

stock = 40
sold = 12
stock = stock - sold
returned = 3
stock = stock + returned
sold = sold + 5
stock = stock - 5
print(stock, sold)

Guided practice · ●●difficulty 2 of 5 · about 4 min

Fix the discount calculator

The discount program from the lesson gives wrong prices. For a 200 SAR item it prints:

Price after 15% discount: 199.85 SAR

but the correct output is:

Price after 15% discount: 170.00 SAR

Fix the logic error. Keep discount = 15 / 100.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Challenge · ●●●difficulty 3 of 5 · about 8 min

Fix the VAT receipt

A shop's receipt program reads a quantity (whole number) and a unit price, then adds 15% VAT. It runs without errors, but the numbers are wrong. For 2 items at 50 SAR it prints:

Subtotal: 100.00 SAR
VAT: 7.50 SAR
Total: 7.50 SAR

The correct output is:

Subtotal: 100.00 SAR
VAT: 15.00 SAR
Total: 115.00 SAR

There are two bugs. Find them by tracing or with temporary print() calls, fix them one at a time, and remove any debug prints before you submit.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Edge cases · ●●difficulty 2 of 5 · about 5 min

Totals that look almost right

A cashier enters the number of items sold in the morning and in the afternoon, and the program prints the day's total. It seemed fine when tested with 0 and 5 (it printed 05, which looked like 5). But for 12 and 30 it prints:

Items sold today: 1230

The correct output is:

Items sold today: 42

Use a debug print with type() if you're not sure what's happening, then fix the program and remove the debug print.

Python code editor. Tab indents. Press Escape then Tab to leave the editor. Control or Command plus Enter runs the code; Shift plus Enter checks your answer.

Ctrl+Enter run · Shift+Enter check · your code is saved in this browser as you type

Output

Run your code to see its output, or Check to test it against the requirements.

The solution becomes available after you pass, or after all three hints and two checks.

Summary

  • A logic error makes a program run but give wrong results.
  • Reproduce the bug with an input whose correct answer you worked out by hand.
  • Trace the code line by line, or add labelled debug prints to see the real values. type() shows what kind of value you have.
  • Change one thing at a time, re-run, test more inputs, and remove debug prints at the end.