Nested Conditionals
Sometimes one decision depends on another. A shipping cost depends on weight and then on destination. Time to put conditionals inside conditionals.
if statements inside other if blocksDecisions 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:
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.
Nest vs. Chain: When to Use Which
You could rewrite the shipping example using and in a flat chain:
Both work! Here's when to prefer each:
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 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.
Challenges
Challenge 1: Shipping Calculator
Calculate shipping cost based on weight, destination, AND membership status.
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.
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
Recommend a movie genre based on two preference questions.
β’ 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.
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
Calculate income tax using progressive brackets β each bracket only taxes income within that range.
β’ 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 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.10elif income <= 40000: β tax = 10000 * 0.10 + (income - 10000) * 0.20else: β tax = 10000 * 0.10 + 30000 * 0.20 + (income - 40000) * 0.30
Challenge 4: Animal Guesser
Build a mini decision tree that identifies an animal using 3 yes/no questions.
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!
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 β catelse:if can_fly: β batelse:if is_big: β whale, else β snake