S09 · Power Up a Device

What we learned in Python

The Python from session S09, one idea at a time. These notes follow lessons/sessions/s09/student/task-card.md in your course folder.

Back to the S09 slides

1 · Python concept

The idea

Today's Python
integer counterA whole-number value that records how many actions have happened. Start count at 0.
updatecount = count + 1 reads the old value, adds one, and stores the new value.
loopRepeats the indented update once for each value supplied by range.
off-by-oneDoing one update too few or too many, so success happens one step too early or late.
assertionassert count == goal checks the expected final count. A false check raises AssertionError; a true check is silent.

2 · Code we wrote

The code

goal = 3
count = 0

for interaction_number in range(1, goal + 1):
    count = count + 1
    print(interaction_number, count)

assert count == goal

Run it from the course folder:

python lessons/sessions/s09/student/starter.py

Output:

1 1
2 2
3 3

3 · What the code means

Line by line

Each important line
goal = 3Stores the intended number of actions as an integer.
count = 0No actions have happened yet.
range(1, goal + 1)Supplies 1, 2, 3. range stops before its second number, so the stop value must be 4.
count = count + 1Each pass changes the state: 0 → 1 → 2 → 3.
assert count == goalChecks that the loop produced exactly the expected count. It catches the shortened loop ending at 2.

4 · Why programmers use this

Beyond this lesson

Counters track repeated actions, such as completed tasks. A loop keeps the update consistent on every pass.

Predict interaction 3 as the first success. Interaction 2 is one spark short. One basic assertion protects the expected local final count.

The canonical starter already has the correct loop and leaves the assertion for you. Add it, then temporarily shorten the range to create the deliberate bug.

Local Python models repeated counting and update logic. Package YAML declares counter.goal and counter.when_goal_reached. Trail owns the actual interaction count during the session; student Python does not execute inside Trail.

The powered Storm Engine prepares the observatory for the next readiness check in the story. This session does not save charging state into a later Trail.

5 · What we debugged

A bug worth understanding

for interaction_number in range(1, goal):
For goal 3, only counts 1 and 2 print. The added assertion raises AssertionError.

The stop value is excluded. Restore goal + 1 for exactly three updates, rerun, and explain why the assertion now passes silently.

Fixed:

for interaction_number in range(1, goal + 1):

6 · Key Python words

Words to know

Vocabulary
integerA whole number, such as 0, 1, 2, or 3.
assignmentStores a value under a variable name.
forRepeats an indented block for each supplied value.
assertChecks a claim about expected behavior.

7 · Try it yourself

Your turn

  1. Predict counts after interactions 1, 2, 3, and 4. Circle the first success interaction before running anything.
  2. Run the starter, write one assertion, shorten the loop, predict and observe the failure, then repair the stop value and rerun.
  3. Validate the package and launch the task-card Trail command. Press E once at a time: at count 2 the mission is incomplete; at count 3 it completes. A later press keeps completion and continues counting.
  4. Explain why the assertion protects the expected count, and which state belongs to local Python, YAML, and Trail.