Tutorial 6/15 Making Decisions

Python: Complex Logic

Master and, or, and elif ladders for sophisticated control flow.

The Hook

Sometimes one check isn't enough.
Think of a VIP Club Bouncer.

  • To enter, you need ID AND a Ticket. (Both must be True)
  • To win a prize, you need a Red Ticket OR Blue Ticket. (Only one needed)
1
Check
ID Card
&
Check
Valid Ticket

Worked Example

We use and, or, and elif (else if) for complex choices.

age = 16
has_ticket = True
if age >= 18 and has_ticket:
# Both need to be True
print("Enter VIP")
elif has_ticket:
# Runs only if first check failed
print("Enter Regular")
else:
# If nothing else worked
print("Go Home")

Trap 1: The Lazy IF

Wrong: if x > 5 and < 10:

Python forgets the variable "x" in the second check!

Fixed: if x > 5 and x < 10:

Trap 2: The Negative OR

Issue: if color != "Red" or color != "Blue":

This is ALWAYS True! (If Red, it's not Blue).

Safe: if color != "Red" and color != "Blue":
Active Recall

Predict Logic

If score is 50, what exactly prints?

if score > 80:
  print("Gold")
elif score > 40:
  print("Silver")
else:
  print("Bronze")

Complex Sandbox

Production Logic Simulator

main.py
Terminal_
> Logic Layer Ready...
Level 13: Bronze

Rollercoaster

"Change the height requirement to 140. Run it to see if you can still ride."

> bronze_stdout_ready
Level 14: Silver

Lazy Logic Fix

"Python forgets 'num' in the second part of the check. Fix the syntax error: and < 20."

> debugger_standby
Level 15: Gold

Secure Login

"Ask for user and password. Print 'Access Granted' ONLY if both 'admin' and '1234' are correct."

> security_module_idle