Making Decisions

Reading, Tracing, and Explaining Programs


Learning Objectives

  • You can predict a program’s output before running it.
  • You can trace an unfamiliar short program line by line and keep track of variable values.
  • You can explain the purpose and behavior of a short program in plain language.
  • You can distinguish a description of syntax from an explanation of behavior.
  • You can evaluate an AI-generated explanation against the code itself.

You now know enough Python to write genuinely interactive programs: they can ask for information, calculate with it, compare values, and choose between different actions. You have already predicted output, followed straight-line assignments, and checked branch boundaries in earlier chapters.

This chapter combines those earlier practices into a systematic way to read a complete program. Instead of mainly asking “how do I write this?”, it asks:

Given a program, can you work out what it does and explain why?

That question matters for debugging, for collaboration, for exams and assessments, for code review, and for using AI assistants responsibly.

Prediction Before Execution

It is tempting to run code immediately. The computer can tell you the answer in a fraction of a second, so why predict?

Because prediction tests your mental model.

If you predict 20 and the program prints 25, you have discovered that something in your understanding was incomplete. That gap is useful. If you run first, you see 25 but may never notice that you would have reasoned incorrectly.

This leads to a recurring workflow:

  1. Predict what the program will do.
  2. Trace the important values and branches.
  3. Run the program.
  4. Compare the actual result with your prediction.
  5. Explain any difference.

Following this pattern will help you read and understand unfamiliar code, whether it is written by a human or generated by an AI assistant. It also helps you learn programming more effectively, because you are actively reasoning about the code instead of passively observing it.

Tracing State and Control Flow Together

Consider:

age = 16
price = 12
if age < 18:
price = 8
print(price)

A trace follows the program in execution order. Unlike the straight-line traces in Chapter 4, this trace must also record whether a condition is true and whether its body runs.

After the first line:

age = 16

After the second line:

age = 16
price = 12

The condition age < 18 is True, so the indented assignment runs:

age = 16
price = 8

Finally, print(price) outputs:

8

The essential habit is to account for each state change and each control-flow decision instead of jumping directly to the final line.

Explain Behavior, Not Just Syntax

Suppose you are asked to explain this program:

minutes = int(input("Minutes: "))
if minutes >= 60:
print("Long session")
else:
print("Short session")

A weak explanation is:

The program makes a variable called minutes, then uses an if, then uses print.

This mostly names syntax.

A stronger explanation is:

The program asks the user for a number of minutes. It treats sessions of 60 minutes or more as long and all shorter sessions as short, then prints the corresponding label.

The stronger explanation communicates purpose, inputs, decision rule, and output.

An Explanation Rubric

Throughout the course, some exercises will ask you to explain unfamiliar code in your own words. A useful explanation normally demonstrates several things:

DimensionWhat a good explanation shows
PurposeWhat problem or task the code addresses
InputsWhat information comes into the program
StateWhat important values are stored or changed
Control flowWhy a particular branch runs
OutputWhat the program produces
BoundariesWhere behavior changes, when relevant

Not every tiny program needs all six dimensions. The rubric is a checklist for understanding, not a template for writing six sentences every time. You do not need to describe every punctuation mark.

An AI feedback tool may assess an explanation and ask follow-up questions. The tool is not meant to reward a particular phrase.

AI Feedback Is Feedback, Not Authority

When an AI tool assesses an explanation, treat the response as another piece of evidence. If the feedback claims something that does not match the code, inspect the code and reasoning rather than automatically rewriting your answer to please the tool.

In higher-stakes assessment, the course should provide a route to human review when an automated evaluation is disputed.


Improve a program explanation

0 / 10 points

Improve this explanation:

“The code has an input, an int, a variable called age, an if and an else.”

The explanation refers to this program:

age = int(input("Age: "))
if age >= 18:
print("You may register.")
else:
print("You need a guardian.")

Explain the program’s purpose and behavior in two or three sentences. Write for someone who has not seen the code, and explain what happens at age 18 rather than merely listing Python syntax.

Explaining State Changes

Some explanations need more than a high-level summary. Consider:

price = 20
has_discount = True
if has_discount:
price = price * 0.8
print(price)

A useful explanation includes the state change:

The program starts with a price of 20. Because has_discount is true, it replaces the price with 80% of its previous value, making the final price 16.0. It then prints 16.0.

The phrase “replaces the price” matters. It shows that you understand assignment changes the current state.

AI Explanations Can Also Be Wrong

Suppose the code is:

number = 8
if number % 2 == 0:
print("even")
else:
print("odd")

An AI assistant explains:

“The program checks whether the number is larger than 2. Because 8 is larger than 2, it prints even.”

The final output happens to be correct, but the explanation is not. % 2 == 0 checks the remainder after division by 2. It has nothing to do with being larger than 2.

This is why you should evaluate explanations against the source code, not against whether the conclusion sounds plausible.

Put It All Together

The final exercise combines input, state changes, branch order, boundaries, and output. Use the rubric to explain the complete program rather than describing its syntax line by line.

Explain a complete score program

0 / 15 points

Explain this program in natural prose:

name = input("Name: ").strip()
score = int(input("Score: "))
if score >= 90:
result = "excellent"
elif score >= 70:
result = "passed"
else:
result = "not passed yet"
print(f"{name}: {result}")

Explain what information the program asks for, how it chooses result, what it prints, and what happens exactly at scores 70 and 90. After receiving feedback, compare every claim in the feedback with the code before revising your response.

Check Your Understanding

  1. What does it mean to trace a program?
  2. Why should you predict before running code during practice?
  3. What is the difference between explaining syntax and explaining behavior?
  4. How can an explanation be wrong even when it predicts the correct output?
  5. What should you do if an AI-generated explanation disagrees with your trace of the program?

AI Study Aid

Create a chapter diagram

We're looking into to what extent AI-generated diagrams could help with studying.

Use this study aid to generate an AI-generated visual summary of the material you just studied. Each diagram style emphasizes the content in a different way, so you can choose the focus that feels most useful.

Using the diagram generator is voluntary and does not affect your course progress. We encourage you to try it out and see if it helps, but it's totally up to you! Your answers help us understand how to make better study aids in the future.

Diagrams tried: 0