Back

Debug Challenge: Syntax & Arithmetic

Difficulty  

divider

Objective

Four short programs. Every one of them is broken. Your job is to find the mistake, say what kind of mistake it is, and fix it.

You did not write these, which is the point. Reading someone else's broken code is a different skill from writing your own, and it is the one you will use most.

Skills to Practice

  • Reading an error message
  • Telling a broken program from a wrong program
  • Order of operations
  • Choosing between / and //

Two Kinds of Mistakes

Not every bug looks the same, and the difference matters because it changes how you hunt for it.

  • A syntax error means Python cannot even read your program. Nothing runs. You get an error message instead of output.
  • A logic error means Python read your program just fine and did exactly what you told it to — but what you told it was wrong. It runs, it prints something, and the something is incorrect. There is no error message at all.

Logic errors are the dangerous ones, because nothing warns you. The only way to catch them is to know what the answer should be and notice that it isn't.

One Warning About Line Numbers

When Python reports a syntax error, it tells you the line where it noticed something was wrong — not always the line where you made the mistake. Those are often different lines, and sometimes Python blames a line number that doesn't even exist in your file.

So treat the line number as a starting point, not an answer. Look at the line it names, then look upward.


The Four Programs

For each one, copy it into a Python file and run it. Then write down:

  • Is it a syntax error or a logic error?
  • What is actually wrong?
  • The corrected line.

Bug 1

Symptom: this program refuses to run at all.

Bug 1
price = 19.99
quantity = 3
print("Total:", price * quantity

Extra question, and it is the important one: which line number does Python blame, and is that the line where the mistake actually is?


Bug 2

Symptom: it runs and prints Area: 20. The rectangle is 12 by 8, so the area should be 96.

Bug 2
length = 12
width = 8
print("Area:", length + width)

Bug 3

Symptom: it runs and prints Average: 206.33333333333334. The average of 88, 92, and 79 should be about 86.33.

Bug 3
score1 = 88
score2 = 92
score3 = 79
print("Average:", score1 + score2 + score3 / 3)

Hint: the number it printed is far too big. Which part of that line does Python do first?


Bug 4

Symptom: it runs and prints Dollars: 12.5. This program is supposed to report how many whole dollars are inside 1250 cents, which is 12.

Bug 4
cents = 1250
print("Dollars:", cents / 100)

Submit

For each of the four bugs, submit:

  • The kind of mistake — syntax or logic
  • One sentence on what was wrong
  • The corrected line of code

Plus your answer to Bug 1's extra question about the line number.

Three of these four are logic errors. If you found yourself waiting for an error message on those, that is exactly the habit this challenge is meant to break.

Commence Debugging