If statements and debugging

25-minute lesson · module-03-debugging

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

If statements and debugging

25-minute lesson · module-03-debugging

  • Use `if`, `elif`, and `else` to make decisions in code.
  • Read and interpret IndentationError, SyntaxError, and TypeError messages.
  • Use CodeLens to step through a branching program one line at a time.
  • Apply a small debugging checklist when something goes wrong.

Asking the program to choose

Sometimes a program needs to do one thing in one situation and a different thing in another. The if statement is how you express that.

temperature = 28
if temperature > 25:
    print("Wear shorts.")
else:
    print("Bring a jacket.")

Three things to notice:

  1. The if line ends with a colon :.
  2. The line(s) under it are indented (four spaces is the convention).
  3. else: runs only when the if condition was False.

elif chains more questions

score = 88
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"
print(grade)

Python checks the conditions top to bottom. As soon as one is True, the rest are skipped — that is why we order them from highest to lowest.

A debugging checklist

When your program is misbehaving, walk through this list before changing random things:

  1. Read the error message — the last line names the error type and

usually the line number.

  1. Predict what the program should do — a one-sentence description

of the expected behavior.

  1. Trace the program by hand or with CodeLens — step through and check

the value of each variable.

  1. Change one thing at a time — and re-run after each change.
  2. If you get stuck, ask the AI tutor — but try predicting first.
Common errors you will meet in this lesson: IndentationError, SyntaxError, and the silent kind — no error, but wrong answer.

Examples

Tell a student whether they passed

score = 72
if score >= 70:
    print("Passed!")
else:
    print("Did not pass.")

The condition `score >= 70` is True when score is 72, so the program prints "Passed!". Try changing score to 65 and predict the output.

Checkpoint questions

What does this program print?

x = 10
if x > 5:
    print("big")
if x > 20:
    print("huge")
else:
    print("not huge")

What is the bug in this code?

score = 85
if score >= 70
    print("Passed!")

Hands-on coding

Letter grade from a score

Pyodide is loading (one-time, ~12 MB)…

ActiveCode
Output
(no output yet)

CodeLens — step through execution

Step through an if/elif/else chain

CodeLens — step through frames, objects, and references

CodeLens
Code · Python 3
1score = 72
2if score >= 90:
3 grade = "A"
4elif score >= 80:
5 grade = "B"
6elif score >= 70:
7 grade = "C"
8else:
9 grade = "F"
10print("Final grade:", grade)
11
line that just executed next line to execute
Print output
(click Trace)

Frames

Click Trace to start.

Objects

no steps yet
Edit the code

Debugging interlude

Predict the error first (+50 XP), then fix the code (+100 XP). Hints cost 30 XP each.

🐞

Find and fix the IndentationError

This program is supposed to print "Yes!" when n is positive, but Python raises IndentationError. Read the error message carefully and fix it.

Predict · 50 XPFix · 100 XP
n = 5
if n > 0:
print("Yes!")
Step 1 — Predict the error

Read the program. Which kind of error (or non-error) will Python raise?

🐞

Logic bug: nothing prints

The student says "the program runs but never prints anything". What's wrong?

Predict · 50 XPFix · 100 XP
score = 50
if score >= 90:
    print("A")
elif score >= 80:
    print("B")
elif score >= 70:
    print("C")
Step 1 — Predict the error

Read the program. Which kind of error (or non-error) will Python raise?

End-of-lesson quiz

End-of-lesson quiz

  1. Which of these is the *correct* way to write an if-else?

  2. When you see `IndentationError: expected an indented block`, what should you do first?

Reflection

  • Describe a real-world situation that you could model with if/elif/else (e.g., decide what to wear, what to eat, how to vote).

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Selection / Debugging Interlude / Boolean values / Logical operators / The if statement / Successful programming habits

Adaptation notes. Reading authored from scratch in our voice. Code examples drawn from common ISU CS101-style examples (grade letter assignment, weather advice). Hidden tests authored specifically for PyTutor AI.

License notes. Verify chapter-level license metadata on Runestone before copying any text verbatim. This lesson contains no verbatim quotations.