← Back to course
Code Crew G6+ / Week 03 / Thursday
4/6
Week 03 Β· Decisions

Thursday

and, or, not
// if, elif, else and the logic words
⏱ about 30 min

Thursday: and, or, not

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.

Joining conditions

WordTrue whenExample
andboth sides are Truefuel >= 80 and weather == "clear"
orat least one side is Trueweather == "windy" or weather == "stormy"
notthe condition is Falsenot 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 conditionTrue or False?
fuel >= 80 and weather == "clear"?
fuel >= 80 or weather == "clear"?
not fuel >= 80?
weather == "windy" or fuel < 50?
WHY THIS EXERCISEand needs both sides True, or needs at least one, and not flips the answer.
PICK THE CONDITION
  • Read the question.
  • Tap your answer.
Launch only when the wind is under 30 and it is not raining. raining is True or False.
Sound the alarm if fuel is under 20 or a door is open.
In go_check.py, change weather to "windy". What does the program show now?
WHY THIS EXERCISEweather == "clear" is now False, so the and condition is False and else runs.
At your computer
1. Type go_check.py in a new file, save it and run it.
2. Change weather to "windy". Run it and check your prediction.
3. Change it back. Set crew_ready = False and run it. You should see two lines.
4. Write one more if that prints Fuel warning when fuel < 50 or weather == "stormy".

Practice: a safe window

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")
INSIDE OR OUTSIDE?
  • Read the question.
  • Tap your answer.
The user types 18. What does it show?
The user types 12. Which part of the or is True?
Can both prints ever show in the same run?
The user types 26. First word shown?
The user types 27. First word shown?
x = 5. What does not x > 3 give?
x = 4. The conditionTrue or False?
x > 1 and x < 5?
x > 5 or x < 1?
not x == 4?
x == 4 or x == 9?
WHY THIS EXERCISEand needs both sides True, or needs at least one, and not turns True into False.
Try it
1. Type safe_window.py, save it and run it with 22, then 30.
2. Test the edges: 17, 18, 26 and 27.
3. Change the window to 20 up to 24 and test the new edges.

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.

← Wednesday