TactivergeLite

Lite / Building Machines That Respond / Week 2

How the interpreter reads your program

You compute current = voltage / resistance, then change the voltage. Does current change too?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Most early bugs come from imagining the program works like a spreadsheet, where cells update themselves. It doesn't. Knowing exactly what the interpreter does on each line lets you predict a program before running it, which is what debugging really is.

Predict

voltage_v = 12, then current_a = voltage_v / 48, then voltage_v = 24. What is current_a now?

  1. 0.25
  2. 0.5
  3. It changes back and forth
Guess first, then reveal

0.25. current_a was calculated once, when its line ran, using the value 12. Changing voltage_v afterwards does not rerun that line.

Discover

Start with 'Battery runtime' and press 'Run next line' one line at a time, watching the variable table. Then try 'Odometer update' (predict distance_m before each step) and 'Used before it exists': which line fails, and why?

What the interactive shows (text version)

The program tracer shows a short Python program, highlights the line just run and lists every variable with its value and type after that line. For example, in 'Battery runtime', after line 3 the table shows capacity_mah 2000 (int), current_ma 250 (int), hours 8.0 (float). In 'Used before it exists', line 2 stops with a NameError because resistance_ohm is created only on line 3.

You can also open the interactive on the full site when you have a faster connection.

Understand

One line at a time, top to bottom

The interpreter starts at line 1. For an assignment it works out the right-hand side using the values that exist right now, then stores the result under the name on the left. Then it moves to the next line. Nothing goes back up unless a loop or function tells it to.

Assignment is a snapshot

voltage_v = 12
resistance_ohm = 48
current_a = voltage_v / resistance_ohm
print(current_a)        # 0.25
voltage_v = 24          # changing it now does not update current_a
print(current_a)        # still 0.25

To get the new current you must run the calculation again.

Names must exist before you use them

If line 2 uses resistance_ohm but line 3 is where it is created, Python stops at line 2 with NameError: name 'resistance_ohm' is not defined. The fix is to move the assignment above the line that needs it. A typo in a name gives the same error.

Explain it another way

Intuitive

The interpreter is a very careful clerk reading a list of instructions, one at a time, with a whiteboard of variable values. It only ever looks at the current line and the whiteboard.

Technical

Python executes statements sequentially. An assignment evaluates its expression in the current namespace and binds the name to the resulting object. A name lookup with no binding raises NameError at runtime.

Applied

Robot control code runs the same idea in a loop: read sensors, compute, act, then compute again with fresh readings. A value computed once at start-up, such as a calibration offset, silently stays stale if the code never recomputes it.

Worked example 1

Trace: speed = 2; time = 5; distance = speed * time; speed = 3; print(distance).

speed 2 → time 5 → distance 10 → speed 3 → prints 10. distance was computed when speed was 2; the later change to speed does not affect it.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 2. a = 5; b = a * 2; a = 100; print(b). What is printed?
Practice 3. Which program raises a NameError?
Practice 4. In Python, what does = mean in total = total + 5?

Apply it

Write a five-line program that computes a robot's distance travelled from speed and time, prints it, changes the speed, recomputes and prints again. Trace it by hand first, then run it in the Code Lab and compare.

Reflect

Describe, in one sentence, the difference between = in maths and = in Python.

Write your answer on paper or in your own notes. Lite never stores what you write.

← Your first Python program: battery runtimeVariables that track a changing machine →