← Back to course
Python 11-12 / Week 01 / Tuesday
2/6
Week 01 · Back at the Terminal

Tuesday

Trace before you run
// Review fast, trace before you run, and name the three kinds of error
⏱ about 30 min

Tuesday: Trace Before You Run

Comet has already run three versions of a loop this morning. Each time the total surprised her.

"I just keep pressing F5 until it looks right," she admits, laughing. "That is faster, isn't it?"

Wren slides his clipboard across the table. It holds a grid with a column for every variable. "What does the evidence say?" he asks. "Your runs show the end. A trace shows every step on the way."

Nova projects an empty trace table beside the code. "How can I help?" she asks. "Fill in one row for each pass, then let the computer check you."

Comet picks up her pencil from behind her ear. "Fine. Lead programmer, you take the first row."

A trace table

A trace table has one column for each variable and one row for each step. You fill it in by hand while you follow the program.

When you trace first, your prediction is ready before the computer answers. A surprise then tells you exactly where your thinking split from the code.

# trace_loop.py
grams = [42, 18, 65]
total = 0
for g in grams:
    total = total + g
print(total)
Stepgtotal
before the loop(no value yet)0
pass 14242
pass 21860
pass 365125
TRACE TRACE_LOOP.PY
  • Read the question.
  • Tap your answer.
What does the program show?
How many times does the line total = total + g run?
How many times does print(total) run?
125

How many times does a line run?

A statement execution count is how many times a line runs. You can work it out by tracing.

Counting this way is a simple, informal way to compare how much work different loops do. Watch the count when one loop sits inside another.

# trace_nested.py
count = 0
for row in range(2):
    for col in range(3):
        count += 1
        print(row, col)
print("count", count)
TRACE TRACE_NESTED.PY
  • Read the question.
  • Tap your answer.
How many times does count += 1 run?
What is the fourth line of output?
What is the last line?
0 0
0 1
0 2
1 0
1 1
1 2
count 6

Dict review: get and items

Comet's sample log will start as dicts tomorrow, so here is a quick review. Predict each line, then check with the run.

kinds["S01"] reads the value stored under the key "S01". The run shows what get and items give back.

# dict_review.py
kinds = {"S01": "basalt", "S02": "quartz"}
print(kinds["S01"])
print(kinds.get("S02"))
print(kinds.get("S09"))
print(kinds.get("S09", "none yet"))
for label, kind in kinds.items():
    print(label, kind)
PREDICT DICT_REVIEW.PY
  • Read the question.
  • Tap your answer.
What does print(kinds.get("S09")) show?
What does print(kinds.get("S09", "none yet")) show?
basalt
quartz
None
none yet
S01 basalt
S02 quartz
At your computer
1. On paper, draw a trace table for trace_loop.py with the columns g and total.
2. Type the program, save it in your fieldsim folder and run it. Check your last row.
3. Add 7 to the end of the list. Predict the new total, then run it.
4. Misspell total as totl in the print line. Run it, read the last line of the message, then fix it.
StatementTrue or false?
A line inside a loop can run more than once.?
In trace_nested.py, count += 1 runs 2 times.?
dict_review.py stops with an error at kinds.get("S09").?
The items() loop gave each label and its kind together.?
WHY THIS EXERCISELoop bodies repeat, the inner body of trace_nested.py runs 6 times, and get gave None instead of stopping.

Tracing is a habit that saves runs and explains surprises. Tomorrow is Sim Lab, and Comet's first FieldSim prototype.

← Monday