S08 · Build an If/Else Guardian

What we learned in Python

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

Back to the S08 slides

1 · Python concept

The idea

Today's Python
conditionA test that evaluates to True or False.
ifRuns its block when the condition is True.
elseRuns its block when the condition is False.
returnGives the selected response back to the caller and ends the function.

2 · Code we wrote

The code

def guardian_response(is_on):
    if is_on:
        return "The portal is glowing!"
    else:
        return "The portal is sleeping."


print(guardian_response(False))
print(guardian_response(True))

Run it from the course folder:

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

Output:

The portal is sleeping.
The portal is glowing!

3 · What the code means

Line by line

Each important line
guardian_response(is_on)Receives the Boolean state as an argument.
if is_on:Selects the glowing response when the argument is True.
else:Selects the sleeping response when the argument is False.
print(guardian_response(False))Calls the function with False and prints the line it returns.

4 · Why programmers use this

Beyond this lesson

A single condition lets the same function choose between two responses.

Testing both inputs reveals a swapped pair of responses even when Python runs without errors.

Python models the branch. The package's respond_to_toggle links the Guardian to the Sky Switch. The Trail reads its own switch state to select when_off or when_on.

5 · What we debugged

A bug worth understanding

if not is_on:
The glowing response prints first, even though the first input is False.

not turns False into True, so the first call enters the if block. Test is_on directly to make True choose the glowing response.

Fixed:

if is_on:

6 · Key Python words

Words to know

Vocabulary
branchOne of the possible paths through a decision.
notReverses a Boolean value.
ifIntroduces the condition and its True branch.
elseIntroduces the fallback branch.

7 · Try it yourself

Your turn

  1. Predict both outputs before running the starter.
  2. Repair only the condition. Run both cases again and explain which line each call returns.
  3. Talk to the Guardian with the switch off, flip the Sky Switch, then talk again. Record both responses and look for Responses 2 / 2.