1 · Python concept
The idea
| condition | A test that evaluates to True or False. |
|---|---|
| if | Runs its block when the condition is True. |
| else | Runs its block when the condition is False. |
| return | Gives the selected response back to the caller and ends the function. |
S08 · Build an If/Else Guardian
The Python from session S08, one idea at a time. These notes follow lessons/sessions/s08/student/task-card.md in your course folder.
1 · Python concept
| condition | A test that evaluates to True or False. |
|---|---|
| if | Runs its block when the condition is True. |
| else | Runs its block when the condition is False. |
| return | Gives the selected response back to the caller and ends the function. |
2 · Code we wrote
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.pyOutput:
The portal is sleeping.
The portal is glowing!3 · What the code means
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
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
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
| branch | One of the possible paths through a decision. |
|---|---|
| not | Reverses a Boolean value. |
| if | Introduces the condition and its True branch. |
| else | Introduces the fallback branch. |
7 · Try it yourself