Unit 2 Β· Lesson 5

Nested Conditionals

Sometimes one decision depends on another. A shipping cost depends on weight and then on destination. Time to put conditionals inside conditionals.

Nest if statements inside other if blocks
Know when to nest vs. when to chain
Decompose complex problems into steps
Build decision-tree programs

Decisions Inside Decisions

So far, your if/elif/else chains have been "flat" β€” one level of decisions. But real problems often require multi-level thinking. Consider shipping costs:

weight = 3.5 # kg destination = "international" if weight <= 2: # Light package if destination == "domestic": cost = 5.00 else: cost = 15.00 else: # Heavy package if destination == "domestic": cost = 10.00 else: cost = 25.00 print(f"Shipping: ${cost:.2f}") # Shipping: $25.00

The outer if checks weight. The inner if (nested inside) then checks destination. Python uses indentation to keep track of which level you're on.

Indentation is your map: Each nested level adds 4 more spaces. The outer if is at 0 spaces, its body at 4, the inner if's body at 8. If you get lost, count the spaces!

Nest vs. Chain: When to Use Which

You could rewrite the shipping example using and in a flat chain:

# Flat version with and β€” also works! if weight <= 2 and destination == "domestic": cost = 5.00 elif weight <= 2 and destination == "international": cost = 15.00 elif weight > 2 and destination == "domestic": cost = 10.00 else: cost = 25.00

Both work! Here's when to prefer each:

Use nesting when: The second decision only makes sense after the first is resolved, or when you want to group related logic together. Think: "First check A, then within that, check B."

Use chaining when: All conditions are independent and you're picking from a flat list of outcomes. If nesting goes deeper than 2–3 levels, consider refactoring to a flat chain.

Problem Decomposition

Complex decisions are easiest when you break them into steps before writing code:

Step 1: Identify the first/biggest decision.
Step 2: For each outcome, ask "is there another decision?"
Step 3: Write the outer if/else first, then fill in the inner logic.
Step 4: Test each path independently.
Practice Time

Challenges

πŸ“¦

Challenge 1: Shipping Calculator

Guided

Calculate shipping cost based on weight, destination, AND membership status.

Instructions: Set cost based on these rules:

Domestic: $5 if weight ≀ 5kg, $10 if heavier
International: $15 if weight ≀ 5kg, $25 if heavier
Members get 50% off the final cost (multiply by 0.5)

Use nested if/else: first check destination, then weight inside each, then apply discount.
Hint 1: Outer if: if destination == "domestic": then inner if checks weight. Same structure for the else (international).
Hint 2: After the nested blocks set cost, add a separate if is_member: check to halve it.
Hint 3: if is_member: then cost = cost * 0.5. This should be at the same level as the outer if β€” not nested inside it.
🎬

Challenge 2: Movie Recommender

Guided

Recommend a movie genre based on two preference questions.

Instructions: Given likes_action and likes_romance (both booleans), set genre:
β€’ Likes action AND romance β†’ "Action Romance"
β€’ Likes action only β†’ "Thriller"
β€’ Likes romance only β†’ "Rom-Com"
β€’ Neither β†’ "Documentary"

Use nested if/else: outer checks action, inner checks romance.
Hint 1: if likes_action: β€” inside, check if likes_romance: β†’ "Action Romance", else β†’ "Thriller".
Hint 2: The outer else: means they don't like action. Inside that, check romance: if yes β†’ "Rom-Com", else β†’ "Documentary".
Hint 3: You could also solve this with and/or in a flat chain β€” both approaches work!
πŸ’°

Challenge 3: Progressive Tax

Solo

Calculate income tax using progressive brackets β€” each bracket only taxes income within that range.

Instructions: Given income, calculate tax using these brackets:
β€’ First $10,000: taxed at 10%
β€’ Next $30,000 ($10,001–$40,000): taxed at 20%
β€’ Anything above $40,000: taxed at 30%

Example: Income of $50,000 β†’ $10,000Γ—0.10 + $30,000Γ—0.20 + $10,000Γ—0.30 = $1,000 + $6,000 + $3,000 = $10,000

Use if/elif/else to determine which brackets apply, then calculate accordingly.
Hint 1: If income ≀ 10000, tax is just income Γ— 0.10. If income ≀ 40000, tax is $1000 (first bracket) + the remaining taxed at 20%.
Hint 2: For income > 40000: tax = 10000 Γ— 0.10 + 30000 Γ— 0.20 + (income - 40000) Γ— 0.30
Hint 3: Three cases:
if income <= 10000: β†’ tax = income * 0.10
elif income <= 40000: β†’ tax = 10000 * 0.10 + (income - 10000) * 0.20
else: β†’ tax = 10000 * 0.10 + 30000 * 0.20 + (income - 40000) * 0.30
🐾

Challenge 4: Animal Guesser

Stretch

Build a mini decision tree that identifies an animal using 3 yes/no questions.

Instructions: Given three boolean answers, set animal using nested conditionals:

Q1: has_legs β€” Does it have legs?
  Q2a (if legs): can_fly β€” Can it fly? β†’ "eagle" if yes, then check Q3
  Q3a (if legs, no fly): is_big β€” Is it big? β†’ "elephant" if yes, "cat" if no
  Q2b (if no legs): can_fly β€” Can it fly? β†’ "bat" if yes (it's special!)
  Q3b (if no legs, no fly): is_big β€” Is it big? β†’ "whale" if yes, "snake" if no

This requires 3 levels of nesting!
Hint 1: Start with if has_legs:, then nest if can_fly: inside that.
Hint 2: The full structure has 3 levels: has_legs β†’ can_fly β†’ is_big (for some paths). The "eagle" case is when has_legs AND can_fly (no need to check is_big).
Hint 3: if has_legs:
  if can_fly:
    animal = "eagle"
  else:
    if is_big: β†’ elephant, else β†’ cat
else:
  if can_fly: β†’ bat
  else:
    if is_big: β†’ whale, else β†’ snake