Skip to content
Tactiverge

School/△ Builder/Week 3

Variables that track a changing machine

A robot adds speed × time to its distance fifty times a second. How does one variable keep track of all that?

About 30 min · 3 goals · Checked by automated tests

1.Why should I care?

Machines are full of quantities that change: parts made, distance travelled, energy used. In code, each is a variable updated over and over. Get the update right and the robot knows where it is; get it wrong and the error grows every step.

  • ◇ Choose clear variable names that include units.
  • ◇ Update a variable from its own previous value (counters and running totals).
  • ◇ Swap two values correctly and explain why the naive swap fails.

2.Predict

left_c = 31.5, right_c = 29.0, then left_c = right_c, then right_c = left_c. What are they now?

Guess first. Being wrong here is how the surprise works.

3.Discover

Run 'Odometer update' and 'Part counter' line by line: which variables change at each step? Then compare 'Swap two sensors (buggy)' with the fixed version and find the exact line where 31.5 is lost.

Loading the interactive…

Can't use the interactive? Read a description instead

In the program tracer, 'Odometer update' shows distance_m changing 0 → 3.0 → 6.0 → 7.0 as lines 4, 5 and 7 run. 'Swap two sensors (buggy)' shows left_c becoming 29.0 on line 3 and right_c staying 29.0 on line 4, so 31.5 is lost; the fixed version stores 31.5 in spare_c first.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A robot starts with distance_m = 0.0 and its loop runs distance_m = distance_m + speed_m_per_s * dt_s exactly 7 times, with speed_m_per_s = 2 and dt_s = 0.1. What is distance_m at the end, in metres?

m

Practice 2

Which lines correctly swap a and b?

5.Understand

Names that explain themselves

distance_m, speed_m_per_s, parts_made say what they are and in which unit. Python names use letters, digits and underscores, cannot start with a digit and are case-sensitive (Speed and speed are different). Clear names are the cheapest documentation there is.

Updating from the old value

parts_made = parts_made + 1 reads the old value, adds 1 and stores the result: a counter. distance_m = distance_m + speed_m_per_s * dt_s is a running total. The short form parts_made += 1 does the same. Both need a starting value first, such as parts_made = 0.

Swapping without losing data

left_c, right_c = 31.5, 29.0
left_c, right_c = right_c, left_c
print(left_c, right_c)   # 29.0 31.5

Python evaluates the whole right side first, then assigns, so nothing is overwritten too early. The step-by-step version needs a spare: spare_c = left_c, then left_c = right_c, then right_c = spare_c.

Explain it another way

A counter is like a tally on a whiteboard: rub out the old number and write one more. You have to read it before you rub it out.

Worked example 1

distance_m = 0; speed 1.5 m/s for two steps of 2 s, then 0.5 m/s for one step of 2 s. Final distance?

0 + 1.5 × 2 = 3.0 → 3.0 + 1.5 × 2 = 6.0 → 6.0 + 0.5 × 2 = 7.0 m. This is the 'Odometer update' program; the tracer prints 7.0 m.

Common mix-ups

  • a = b then b = a swaps two values.
    ✓ The first line overwrites a, so its old value is gone. Use a spare or a, b = b, a.
  • Short names like x and t make code tidier.
    ✓ In engineering code, names with units (time_s, temp_c) prevent mistakes that cost far more than a few keystrokes.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

Which is a valid Python variable name?

Practice 4

count = 0, then count += 1 runs 3 times, then count = count * 2 runs once. What is count?

Practice 5

A program runs total_wh = total_wh + 5 but never set total_wh before. What happens?

7.Apply it

Write a program for a conveyor: start parts_made and rejects at 0, then record eight parts (three of them rejected) with updates. Print the number of good parts and the reject rate as a percentage. Trace it by hand, then run it.

8.Reflect

Concepts: Variables, types and expressions