S10 · Check the Boundary

What we learned in Python

The Python from session S10, one idea at a time. The same notes are in your course folder as lessons/sessions/s10/student/python-notes.md.

Back to the S10 slides

1 · Python concept

The idea

Today's Python
functionA named block of code that can receive inputs and produce a result.
returnSends a value back to the code that called the function.
>=Means greater than or equal to. Equality is included, so the exact goal is True.
boundary caseA value at or immediately beside the point where a rule changes result.
assertionExecutable evidence that a result matches the value you expected.

2 · Code we wrote

The code

def at_goal(count, goal):
    return count >= goal


goal = 3
print(at_goal(goal - 1, goal))
print(at_goal(goal, goal))
print(at_goal(goal + 1, goal))

assert at_goal(goal - 1, goal) is False
assert at_goal(goal, goal) is True
assert at_goal(goal + 1, goal) is True

Run it from the course folder:

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

Output:

False
True
True

3 · What the code means

Line by line

Each important line
def at_goal(count, goal):Defines a function with the current count and the target goal as inputs.
return count >= goalReturns True when count is equal to or greater than goal; otherwise it returns False.
goal - 1One below the boundary. For goal 3, count 2 returns False.
goalThe exact boundary. For goal 3, count 3 returns True because >= includes equality.
goal + 1One above the boundary. For goal 3, count 4 returns True.

4 · Why programmers use this

Beyond this lesson

Predict all three results before revealing or running the comparison. That makes the program a test of your reasoning instead of a source of answers.

Threshold checks appear in battery meters, scores, inventory levels, ages, temperatures, and many other programs. The exact boundary decides whether just enough counts.

Three assertions preserve evidence for below, exact, and above the goal. A passing assertion is silent; a false assertion raises AssertionError.

Local Python models the rule. Declarative YAML links the Weather Reader to the same-package counter and stores its two responses. Trail owns the fresh interaction state and evaluates the counter's authored goal; it never runs this Python file.

5 · What we debugged

A bug worth understanding

return count > goal
The exact-goal assertion fails: at_goal(3, 3) returns False.

> excludes equality. The below-goal and above-goal cases still agree with their assertions, so the exact-goal case is the evidence that exposes this boundary bug.

Fixed:

return count >= goal

6 · Key Python words

Words to know

Vocabulary
comparisonAn expression such as count >= goal that evaluates to True or False.
BooleanOne of the two values True and False.
boundaryThe exact point where the result changes from False to True.
assertChecks an expected result and raises AssertionError when the check is false.

7 · Try it yourself

Your turn

  1. Before changing the starter, write and explain predictions for goal - 1, goal, and goal + 1. Then reveal >= and compare the three printed results with your predictions.
  2. Add all three assertions. Temporarily change >= to >, identify why only the exact-goal assertion fails, then restore >= and rerun.
  3. Validate and launch the task-card Trail. Speak to the Weather Reader below goal, charge the Storm Engine to exactly 3, and speak again. Record both responses and Responses 2 / 2.
  4. Explain which evidence belongs to Python, which values are declarative YAML, and which count exists only in this fresh Trail run.