Skip to content

06 Multi-Condition Logic

Sometimes one condition is not enough. In this notebook, we will check two simple conditions at the same time.

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.

View original source

Learning Objectives

By the end, you should be able to:

  • Review how an if statement checks True or False.
  • Use and when both conditions must be True.
  • Use or when at least one condition must be True.
  • Build a small eligibility or classification program.

In the previous notebook, you used one condition to make decisions. Now, you will handle situations where more than one condition matters.

Review of Decision-Making (Module 5)

An if statement checks a condition. If the condition is True, the indented code runs.

Think Before You Run: Will the message print when disk_usage is 82?

# Review: one condition
disk_usage = 82

if disk_usage > 80:
    print("Disk usage warning")

Introducing Multiple Conditions

Some decisions need more than one check. For example, access may require both an ID card and an active account.

AND Logic

and means both conditions must be True. If only one condition is True, the whole check is False.

Think Before You Run: The user has an ID card and an active account. Should access be allowed?

# AND example: system access control
has_id_card = True
has_active_account = True

if has_id_card and has_active_account:
    print("Access allowed")
else:
    print("Access denied")

Think Before You Run: What happens if only one condition is True?

# AND example: one condition is False
has_id_card = True
has_active_account = False

print("AND result:", has_id_card and has_active_account)

Guided Practice (AND)

Change one value at a time. Notice that both values must be True for access to be allowed.

Think Before You Run: Which condition fails when login needs another step?

# TODO: Try changing has_password and has_security_code.
has_password = True
has_security_code = False

if has_password and has_security_code:
    print("Login approved")
else:
    print("Login needs another step")

OR Logic

or means at least one condition must be True. The whole check is False only when both conditions are False.

Think Before You Run: If either security warning is True, should the alert print?

# OR example: security alert
many_failed_logins = False
unknown_location = True

if many_failed_logins or unknown_location:
    print("Security alert")
else:
    print("No alert")

Think Before You Run: What happens if both conditions are False?

# OR example: both conditions are False
many_failed_logins = False
unknown_location = False

print("OR result:", many_failed_logins or unknown_location)

Guided Practice (OR)

Change the values and rerun the cell. At least one True value should trigger the message.

Think Before You Run: Which part controls whether the warning appears?

# TODO: Try all combinations of True and False.
low_battery = False
low_storage = True

if low_battery or low_storage:
    print("Device needs attention")
else:
    print("Device looks okay")

Comparing AND vs OR

The same two values can give different results with and and or. and is stricter. or is more flexible.

Think Before You Run: With one True and one False, what will and do? What will or do?

# Side-by-side comparison
has_network = True
has_admin_permission = False

print("AND result:", has_network and has_admin_permission)
print("OR result:", has_network or has_admin_permission)

Think Before You Run: Which line will be True, and which line will be False?

# Side-by-side comparison with score and attendance
score = 60
attendance = 70

print(score >= 50 and attendance >= 75)
print(score >= 50 or attendance >= 75)

With and, both requirements must pass. With or, one passing requirement is enough.

Mini visual:

AND:
True AND True -> True
True AND False -> False

OR:
True OR False -> True
False OR False -> False

Real-World Examples

Multiple conditions help with IT decisions such as system access control, security alerts, login attempts, and resource monitoring.

# Resource monitoring example
disk_usage = 86
memory_usage = 72

if disk_usage > 80 or memory_usage > 80:
    print("Resource warning")
else:
    print("Resources look okay")

# Access control example
completed_training = True
signed_policy = True

if completed_training and signed_policy:
    print("User can receive system access")
else:
    print("User must finish requirements first")

Think Before You Run: Which security condition causes the alert?

# Security alert example
failed_login_attempts = 5
unusual_location = False

if failed_login_attempts > 3 or unusual_location:
    print("Security review needed")
else:
    print("No security review needed")

Common Mistakes

Common mistakes include confusing and with or, assuming one True is enough for and, and writing overly complex conditions. Start simple. Test one condition at a time.

# Mistake to avoid: using and when one warning should be enough.
low_storage = True
low_battery = False

# For alerts, or often makes more sense because either issue matters.
if low_storage or low_battery:
    print("Show warning")
else:
    print("No warning")

Parsons Problem

The lines below are scrambled. Put them in the correct order in the next cell.

else:
has_active_account = True
if has_id_card and has_active_account:
    print("Access allowed")
has_id_card = True
    print("Access denied")
# TODO: Reorder the scrambled lines from above.
# Create the values first, then write the decision.

has_id_card = True
has_active_account = True

if has_id_card and has_active_account:
    print("Access allowed")
else:
    print("Access denied")

Mini Lab

Build a simple eligibility system using multiple conditions. Keep it short and test by changing the values.

# Mini Lab: student access eligibility
# Step 1: Set up the required values.
# Step 2: Use AND to check both requirements.
# Step 3: Print a clear result.
# TODO: Change the values and rerun the program.

completed_orientation = True
has_course_account = True

# Both required items must be complete.
if completed_orientation and has_course_account:
    print("Student is eligible for notebook access")
else:
    print("Student must finish setup first")

Light Combined Example (end only)

This final example combines and and or once. Read it slowly and focus on the parentheses.

Think Before You Run: Which part inside the parentheses controls the result?

# One simple combined example
score = 60
attendance = 70

if score >= 50 and (attendance >= 75 or score > 80):
    print("Eligible")
else:
    print("Not eligible yet")

Wrap-Up

and checks multiple requirements. or allows flexibility when one condition is enough.

These ideas help handle real-world rules. Next, you will repeat these checks using loops.

Reflection

Where could and or or logic help make a real-world decision in your field or daily work?