3

Lesson 3: Conditionals and Decision Making

Making programs that can make decisions

Learning Objectives

By the end of this lesson, students will:

  • Use if, elif, and else statements effectively
  • Apply comparison operators (==, !=, <, >, <=, >=)
  • Combine conditions with logical operators (and, or, not)
  • Create nested conditional structures

Skills Developed:

  • Logical thinking and decision trees
  • Program flow control
  • Boolean logic understanding

Lesson Content

1 Basic If Statements (15 minutes)

Key Concepts:

  • • If statements let programs make decisions
  • • Code inside if block runs only when condition is True
  • • Indentation is crucial in Python
  • • Conditions evaluate to True or False
# Basic if statement
age = int(input("Enter your age: "))
if age >= 16:
print("You can drive!")
print("This always runs")
# Multiple conditions
temperature = 75
if temperature > 80:
print("It's hot outside!")
if temperature < 60:
print("It's cold outside!")

Instructor Notes:

Emphasize indentation! Show what happens with incorrect indentation. Use real-world decision examples like "If it's raining, take an umbrella."

2 Comparison Operators (15 minutes)

Comparison Operators:

  • == equal to
  • != not equal to
  • < less than
  • > greater than
  • <= less than or equal
  • >= greater than or equal

Common Mistakes:

  • • Using = instead of ==
  • • Comparing different types
  • • Case sensitivity with strings
  • • Forgetting quotes around strings
# Comparison examples
score = 85
if score >= 90:
print("A grade!")
name = input("Enter your name: ")
if name == "Python":
print("That's a programming language!")
# Be careful with strings
if name.lower() == "python": # Case insensitive
print("Found Python!")

3 If-Elif-Else Chains (20 minutes)

Decision Chain Structure:

  • if: First condition to check
  • elif: Additional conditions (else if)
  • else: Default action if nothing else matches
  • • Only ONE block executes in the entire chain
# Grade calculator
score = int(input("Enter your test score: "))
if score >= 90:
print("Excellent! You got an A")
elif score >= 80:
print("Good job! You got a B")
elif score >= 70:
print("Not bad! You got a C")
elif score >= 60:
print("You passed with a D")
else:
print("You need to study more. F grade.")

Teaching Tip:

Draw a flowchart on the board showing how the program flows through the conditions. Only one path is taken!

4 Logical Operators and Nested Conditions (15 minutes)

Logical Operators:

and:

Both conditions must be True

or:

At least one condition must be True

not:

Reverses True/False

# Logical operators
age = 17
has_license = True
if age >= 16 and has_license:
print("You can drive!")
weather = "sunny"
temperature = 75
if weather == "sunny" or temperature > 80:
print("Great day to be outside!")
# Nested conditions
if age >= 13:
if age < 20:
print("You're a teenager!")

Hands-On Activity (25 minutes)

Project: Robot Decision System

Students will create a program that helps a robot make decisions based on sensor readings and conditions.

Requirements:

  • • Ask for robot's battery level (0-100)
  • • Ask for obstacle distance (in cm)
  • • Ask if it's daytime (yes/no)
  • • Use if-elif-else to determine robot action
  • • Include at least one logical operator (and/or)
  • • Add comments explaining the decision logic

Decision Logic:

  • • Battery < 20%: "Return to charging station"
  • • Obstacle < 10cm: "Stop and turn around"
  • • Battery > 50% and daytime: "Continue mission"
  • • Battery > 50% and nighttime: "Activate night vision"
  • • Otherwise: "Conserve energy and wait"

🤖📱 How to Code This Project (Choose Your Platform):

📱 Pythonista 3:
  1. Open Pythonista 3
  2. Tap "+" → "Empty Script"
  3. Name it "robot_decision.py"
  4. Type the code below
  5. Tap ▶️ to run
  6. Test different inputs
Tip: Test edge cases (battery=19, distance=9)
🤖 Pydroid 3:
  1. Open Pydroid 3
  2. Tap "+" → "Python file"
  3. Name it "robot_decision.py"
  4. Type the code below
  5. Tap ▶️ yellow play button
  6. Test different inputs
Tip: Use "Terminal" for quick testing
💻 PC/Mac:
  1. Open IDLE or editor
  2. Create new file
  3. Save as "robot_decision.py"
  4. Type the code below
  5. Press F5 to run
  6. Test different inputs
Tip: Run multiple times to test logic
# Robot Decision System
print("Robot Status Check")
battery = int(input("Battery level (0-100): "))
distance = int(input("Obstacle distance (cm): "))
daytime = input("Is it daytime? (yes/no): ").lower()
# Decision logic
if battery < 20:
print("🔋 Return to charging station")
elif distance < 10:
print("🛑 Stop and turn around")
elif battery > 50 and daytime == "yes":
print("🚀 Continue mission")
elif battery > 50 and daytime == "no":
print("🌙 Activate night vision")
else:
print("⏸️ Conserve energy and wait")

Assessment & Homework

Quick Check (In Class):

  • • Can student write proper if-elif-else chains?
  • • Do they understand comparison operators?
  • • Are they using logical operators correctly?
  • • Is their code properly indented?

Homework Assignment:

Smart Home Controller

Create a program that controls smart home devices based on time, temperature, and occupancy. Use nested conditions and logical operators to make intelligent decisions about lighting, heating, and security.

← Previous: Lesson 2
📝 Take Lesson 3 Quiz Next: Lesson 4 →