The launch rule on the wall has three parts: fuel at 80 or more, clear weather, and a crew that says ready. All three must be true at the same time.
Rocket starts writing three nested if statements, one inside another, and the code drifts further and further to the right.
Raven tilts her head at the staircase of indents. "What do you notice about the rule on the wall?" she asks. "It uses the word and. Twice."
Nova traces the word and in glowing letters. "Would you like a hint?" she asks. "Python understands and, or and not. You can join conditions in one line."
Rocket deletes the staircase and writes one tidy line.
| Word | True when | Example |
|---|---|---|
| and | both sides are True | fuel >= 80 and weather == "clear" |
| or | at least one side is True | weather == "windy" or weather == "stormy" |
| not | the condition is False | not crew_ready |
# go_check.py
fuel = 85
weather = "clear"
crew_ready = True
if fuel >= 80 and weather == "clear" and crew_ready:
print("GO for launch")
else:
print("HOLD")
if not crew_ready:
print("Waiting for the crew") With these values, the first if is True, so it shows GO for launch. The condition not crew_ready is False, so the last print is skipped.
GO for launch
| fuel = 70, weather = "clear". The condition | True or False? |
|---|---|
| fuel >= 80 and weather == "clear" | ? |
| fuel >= 80 or weather == "clear" | ? |
| not fuel >= 80 | ? |
| weather == "windy" or fuel < 50 | ? |
and is perfect for checking that a value sits inside a window. The value must be at least the low end and at most the high end.
or is perfect for spotting a value outside the window: too low or too high.
# safe_window.py
temp = int(input("Cabin temperature: "))
if temp >= 18 and temp <= 26:
print("Cabin is comfortable")
if temp < 18 or temp > 26:
print("Adjust the heater") | x = 4. The condition | True or False? |
|---|---|
| x > 1 and x < 5 | ? |
| x > 5 or x < 1 | ? |
| not x == 4 | ? |
| x == 4 or x == 9 | ? |
You can now describe real rules in one clear line. Tomorrow you will build a go or no-go poll that asks the user every question.