Education digital-technologies year-9
Chapter 7 - Loops
A lesson on repetition, validation, and structuring repeated actions in Python.
Chapter 7 introduces repetition, especially for validation and repeated daily logging in the Explorer program.
Chapter Navigation
Chapter 7 — Choosing between options
EXPLORER · v6 · reacting to situations, not just states
A quick reality check
Right now, your console can make one kind of decision.
If the system is online, it says so.
If it is offline, it reacts differently.
That is useful, but it is also very blunt. Real situations are rarely just “yes” or “no”.
If the system is online, it says so.
If it is offline, it reacts differently.
That is useful, but it is also very blunt. Real situations are rarely just “yes” or “no”.
Think about this instead
Imagine you are heading out on a field trip.
If it is snowing, plans change.
If it is extremely hot, plans change in a different way.
If the weather is fine, everything runs as normal.
Notice what is happening here. You are not asking one yes or no question, instead, you are comparing values and choosing the best response.
If it is snowing, plans change.
If it is extremely hot, plans change in a different way.
If the weather is fine, everything runs as normal.
Notice what is happening here. You are not asking one yes or no question, instead, you are comparing values and choosing the best response.
This is where comparisons come in
Python can compare numbers in the same way you do in real life.
It can check whether one number is bigger than another.
It can check whether two numbers are the same.
When Python compares values, it always ends up with a simple answer: True or false.
That answer can then be used to decide what the program does next.
It can check whether one number is bigger than another.
It can check whether two numbers are the same.
When Python compares values, it always ends up with a simple answer: True or false.
That answer can then be used to decide what the program does next.
When there is more than one sensible response
Sometimes two options are not enough. Python handles this by checking conditions one at a time, from top to bottom.
It starts with
If that is not true, it moves on to
If none of the conditions match,
Python stops checking as soon as it finds the first true condition.
It starts with
if. If that is not true, it moves on to
elif. If none of the conditions match,
else becomes the fallback. Python stops checking as soon as it finds the first true condition.
EXPLORER v6 (run this)
The console now reacts differently depending on conditions.
Nothing major has changed in the structure - what has changed is how 'thoughtful' its responses are.
Nothing major has changed in the structure - what has changed is how 'thoughtful' its responses are.
"""
EXPLORER v6 — Chapter 7
This version compares values and chooses between
several reasonable responses.
"""
# =========================
# 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:")
if not system_online:
print("System offline. Mission cannot proceed.")
else:
if temperature_c > 35:
print("Warning: Conditions unsafe. Field work halted.")
elif temperature_c > 25:
print("Caution: Warm conditions. Monitor team closely.")
else:
print("Conditions normal. Proceed as planned.")
print("=" * 44)
What you should notice
The console is more specific than before as small differences in numbers now lead to different outcomes.
This is how programs start to become responsive.
This is how programs start to become responsive.
Next time
Right now, the console reacts once and then stops.
In the next chapter, we will teach it to repeat actions and keep running using loops.
In the next chapter, we will teach it to repeat actions and keep running using loops.