Education digital-technologies year-9
Chapter 6 - Conditional Logic
An introduction to conditional logic and decision-making in Python programs.
Chapter 6 is where the program starts making decisions. It introduces conditions, branches, and indentation discipline.
Chapter Navigation
Chapter 6 — Making decisions
EXPLORER · v5 · teaching the console how to react
Where we are now
The console can now ask the user for information and store it correctly.
It knows the mission location, the day number, and the temperature.
At the moment though, it treats every situation the same way.
No matter what the user enters, the output never changes its behaviour.
It knows the mission location, the day number, and the temperature.
At the moment though, it treats every situation the same way.
No matter what the user enters, the output never changes its behaviour.
Programs need to make choices
Real programs do not just display information.
They respond differently depending on what is happening.
For example:
If the system is online, show one message.
If it is offline, show a different one.
In Python, decisions like this are made using
They respond differently depending on what is happening.
For example:
If the system is online, show one message.
If it is offline, show a different one.
In Python, decisions like this are made using
if statements.Decisions depend on true or false
An
These true or false values are called booleans.
You met booleans in the last chapter when you stored whether the system was online.
Now we are going to use that value to control what the program does.
if statement always checks something that is either True or False. These true or false values are called booleans.
You met booleans in the last chapter when you stored whether the system was online.
Now we are going to use that value to control what the program does.
How an
if statement worksAn
If the answer is
If the answer is
Indentation matters. Python uses indentation to know which lines belong to the decision.
if statement asks a question. If the answer is
True, Python runs the indented code underneath it. If the answer is
False, that code is skipped. Indentation matters. Python uses indentation to know which lines belong to the decision.
EXPLORER v5 (run this)
This version of the console reacts to the system state.
Read the comments and watch how the output changes.
Read the comments and watch how the output changes.
"""
EXPLORER v5 — Chapter 6
This version uses if statements to make decisions
based on boolean values.
"""
# =========================
# COLLECT MISSION DETAILS
# =========================
mission_location = input("Enter mission location: ")
mission_day_text = input("Enter mission day (whole number): ")
mission_day = int(mission_day_text)
temperature_text = input("Enter temperature (°C): ")
temperature_c = float(temperature_text)
# =========================
# SYSTEM STATE
# =========================
system_online = True
# =========================
# CONSOLE OUTPUT
# =========================
print("=" * 44)
print(" EXPLORER CONSOLE ")
print("=" * 44)
print("Location: " + mission_location)
print("Day: " + str(mission_day))
print("Temperature: " + str(temperature_c) + " °C")
print()
print("System status:")
# Decision based on a boolean value
if system_online:
print("Online: System is operational")
else:
print("Online: System is offline")
print("=" * 44)
Why indentation matters
Notice that the lines under
That indentation tells Python which lines belong to each decision.
If the indentation is wrong, Python will either stop with an error or behave in a way you did not expect.
This is one of the most common mistakes beginners make, and it gets easier with practice.
if and else are indented. That indentation tells Python which lines belong to each decision.
If the indentation is wrong, Python will either stop with an error or behave in a way you did not expect.
This is one of the most common mistakes beginners make, and it gets easier with practice.
Try this
Change
Run the program again.
Observe which message is printed and which one is skipped.
The program structure stays the same, but the behaviour changes.
system_online to False. Run the program again.
Observe which message is printed and which one is skipped.
The program structure stays the same, but the behaviour changes.
Next time
Right now, the console can make simple yes or no decisions.
In the next chapter, we will use comparisons and user input to make more flexible decisions.
In the next chapter, we will use comparisons and user input to make more flexible decisions.