05 Decision Making¶
Programs can make choices. In this notebook, we will use simple conditions to choose what message should appear.
Run in Binder Open in Colab Download notebook
Read here. Practice in a notebook.
This page shows the original notebook. Open Binder or Colab to run and edit cells. Work one cell at a time: some examples are deliberately broken for debugging practice. Download your work before leaving Binder; its sessions are temporary.
Learning Objectives¶
By the end, you should be able to:
- Explain that a condition is checked as
TrueorFalse. - Write a simple
ifstatement. - Use
if-elseandif-elif-elsefor more than one path. - Build a small decision program.
In the previous notebook, you used comparison operators to get True or False results. In this notebook, you will use those results to make decisions in a program.
Concept Introduction (no code first)¶
People make decisions all day: if it is raining, bring an umbrella. Programs can do the same kind of checking.
A program checks a condition. If the condition is True, one action happens. If it is False, a different action may happen.
Single Condition (if)¶
if statements use conditions that evaluate to True or False. If the condition is True, the indented block runs. If the condition is False, the block is skipped.
Think Before You Run¶
The disk usage is 85. Will Python print the warning message? What will still print even if the condition is False?
# Single condition example: disk usage warning
# This script checks the disk usage and prints a warning if it exceeds 80%.
# The condition inside an if statement must evaluate to True or False.
disk_usage = 85
if disk_usage > 80:
print("Warning: disk usage is high")
print("Disk check complete")
if-else¶
if-else gives Python two paths. With if-else, exactly one path will run.
Think Before You Run¶
The account is active. Which message do you expect?
# System access example
account_active = True
# If a variable already stores True or False, Python can check it directly.
if account_active:
print("Access allowed")
else:
print("Access denied")
if-elif-else¶
elif means "else if." Python checks the conditions from top to bottom and runs the first matching block.
Think Before You Run¶
There are 4 login attempts. Which message should appear?
# Login attempts example
login_attempts = 4
if login_attempts < 3:
print("Login activity looks normal")
elif login_attempts == 3:
print("Warning: one more failed attempt may lock the account")
else:
print("Account needs support")
Flow Visualization¶
A decision works like a simple path:
- Check the condition.
- If
True: run this block. - Otherwise: check the next option or use
else. - Continue the program.
Check condition
-> if True: run this block
-> otherwise: check next option or use else
-> continue program
Real-World Examples¶
Decision-making appears in many IT tasks, such as checking access, storage, and login activity.
# Example 1: system access
has_id_card = True
if has_id_card:
print("User can enter the lab")
else:
print("User needs an ID card")
# Example 2: disk usage threshold
disk_usage = 78
if disk_usage > 90:
print("Critical disk warning")
elif disk_usage > 80:
print("Disk warning")
else:
print("Disk usage looks okay")
Common Mistakes¶
These mistakes are common. Focus on what changed in the corrected version.
Missing colon:
# Broken
if disk_usage > 80
# Fixed
if disk_usage > 80:
Wrong indentation:
# Broken
if account_active:
print("Access allowed")
# Fixed
if account_active:
print("Access allowed")
Using = instead of ==:
# Broken
if login_attempts = 3:
# Fixed
if login_attempts == 3:
If you get stuck, compare your code line by line with a working pattern.
# Correct pattern to copy when you are unsure.
login_attempts = 3
if login_attempts == 3:
print("Three attempts recorded")
Guided Practice¶
Change one value at a time, rerun the cell, and observe how the output changes.
# TODO: Test a value below the threshold, such as 70.
# TODO: Test a value at a warning level, such as 85.
# TODO: Test a value above the critical level, such as 95.
disk_usage = 85
if disk_usage > 90:
print("Critical: clean up storage now")
elif disk_usage > 80:
print("Warning: storage is getting high")
else:
print("Storage looks okay")
# TODO: Change is_registered to False and rerun.
# TODO: Change it back to True and compare the output.
is_registered = True
if is_registered:
print("Student can access the course site")
else:
print("Student should contact support")
Parsons Problem¶
The lines below are scrambled. Put them in the correct order in the next cell.
Reminder: the variable should come first, the condition line should come before the print lines, and indentation matters.
else:
if attempts > 3:
print("Send password reset help")
attempts = 4
print("Login attempts are okay")
# TODO: Reorder the scrambled lines from above.
# Start by creating the variable, then write the decision.
attempts = 4
if attempts > 3:
print("Send password reset help")
else:
print("Login attempts are okay")
Mini Lab¶
Build a simple course site eligibility program using if-else. Complete the small TODO before running.
# Mini Lab: course site eligibility
# Step 1: store the student name
# Step 2: store the completion status
# Step 3: write the decision
# Step 4: print the result
student_name = "Ava"
# TODO: Change this to True or False before running.
completed_orientation = False
if completed_orientation:
print(student_name, "can continue to the next notebook")
else:
print(student_name, "should finish the orientation first")
Wrap-Up¶
Conditions are checked as True or False. if statements control what the program does next.
In real systems, decisions like access control and alerts rely on this same logic
Next, you will work with multiple conditions and more complex logic.
Reflection¶
Where might decision logic be useful in your field or daily work?