3.07 Nested Conditionals
Put decisions inside decisions and trace multi-level logic.
- Lesson Plan
- Popcorn Hacks & Practice Tasks
- Popcorn Hack 1: After-School Homework Plan (Beginner)
- Code Runner Challenge
- Popcorn Hack 2: Repair the Student Account Check (Intermediate)
- Code Runner Challenge
- Popcorn Hack 3: Lunch and Sports Practice (Advanced)
- Code Runner Challenge
- Homework Hack: High School Daily Routine Dashboard
- Code Runner Challenge
A nested conditional is an if statement inside another if, elif, or else block. The outer decision chooses a path first, and the inner decision checks a more specific condition inside that path. This lesson uses one theme from start to finish: the accounts, homework, meals, and sports that shape a high school student’s daily routine.
Lesson Plan
Learning Objective: Use nested if statements in Python when a student-life decision should happen only after an earlier account or schedule condition selects a path.
Success Criteria: You can trace a nested conditional, explain how indentation controls its branches, write a student-life decision with at least three outcomes, and test every meaningful path.
Lesson Theme — High School Daily Life Dashboard
The lesson follows one student through a normal day:
- Accounts: log in before checking page permissions.
- Homework: check whether work is assigned before checking whether it is finished.
- Meals: plan lunch or a snack around the school and practice schedule.
- Sports: coordinate practice with homework and dinner.
Tech Talk 1: Student Account Decisions
The outer condition is checked first. The inner condition is reached only when execution enters the block that contains it.
has_account = True
password_correct = False
if has_account:
if password_correct:
print("Login approved")
else:
print("Incorrect password")
else:
print("Create an account first")
Python checks has_account before it checks password_correct. If has_account is False, the password condition is skipped because it belongs inside the first branch.
Tech Talk 2: Homework Before Dinner
Python uses indentation to show which statements belong together. Here, the program checks whether homework exists before deciding whether the student can move on to dinner or free time.
homework_assigned = True
homework_finished = False
if homework_assigned:
if homework_finished:
print("Homework finished — time for dinner")
else:
print("Finish homework before dinner")
else:
print("No homework tonight — check tomorrow's schedule")
This homework example has three possible paths, but only one message prints during a run. Moving the inner else to the wrong indentation level would change which if statement it belongs to.
Tech Talk 3: Nested and Compound Conditions Are Different
A compound condition checks multiple Boolean expressions at the same time. A nested conditional makes the second check depend on the first path.
| Purpose | Python example |
|---|---|
| Both rules must be true for one result | if practice_today and homework_finished: |
| Check homework only when practice is scheduled | if practice_today: then nested if homework_finished: |
The same nested structure appears in three forms:
| Form | Outer and inner decision |
|---|---|
| Python | if condition_a: then indented if condition_b: |
| JavaScript | if (conditionA) { if (conditionB) { ... } } |
| College Board pseudocode | IF(conditionA) { IF(conditionB) { ... } } |
Popcorn Hacks & Practice Tasks
Popcorn Hack 1: After-School Homework Plan (Beginner)
Start by tracing completed code. Predict whether the student gets free time, must finish homework, or follows a weekend plan. Then run the code and test every path.
Popcorn Hack 1 - Test the after-school homework plan
is_weekday = True
homework_finished = False
if is_weekday:
if homework_finished:
print("Free time")
else:
print("Finish homework")
else:
print("Weekend plan")
Code Runner Challenge
Popcorn Hack 1 - Test the after-school homework plan
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Test the after-school homework plan
is_weekday = True
homework_finished = False
if is_weekday:
if homework_finished:
print("Free time")
else:
print("Finish homework")
else:
print("Weekend plan")
Do these three things:
- Write the message that will print.
- Name the outer branch and inner branch the program follows.
- Change the values to reach each of the other two outcomes.
Popcorn Hack 2: Repair the Student Account Check (Intermediate)
Next, debug existing code. The student dashboard should check has_permission only after the student logs in, but the permission decision is not indented correctly.
Popcorn Hack 2 - Repair the student account check
logged_in = True
has_permission = False
if logged_in:
print("Account found")
if has_permission:
print("Page opened")
else:
print("Permission needed")
Code Runner Challenge
Popcorn Hack 2 - Repair the student account check
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Repair the student account check
logged_in = True
has_permission = False
if logged_in:
print("Account found")
if has_permission:
print("Page opened")
else:
print("Permission needed")
Do these four things:
- Explain why the current code is not nested.
- Move the permission check inside the logged-in branch.
- Add an outer
elsethat printsLog in first. - Test all three possible messages.
Popcorn Hack 3: Lunch and Sports Practice (Advanced)
Now complete a partial solution. A student needs to plan lunch and decide whether to bring an extra snack for after-school practice. Add the missing inner decisions, test all four paths, and then translate the finished structure.
Popcorn Hack 3 - Test the lunch and sports-practice plan
lunch_packed = True
practice_after_school = True
if lunch_packed:
# TODO: Add a nested conditional that checks practice_after_school.
pass
else:
# TODO: Add another nested conditional that checks practice_after_school.
pass
Code Runner Challenge
Popcorn Hack 3 - Test the lunch and sports-practice plan
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - Test the lunch and sports-practice plan
lunch_packed = True
practice_after_school = True
if lunch_packed:
# TODO: Add a nested conditional that checks practice_after_school.
pass
else:
# TODO: Add another nested conditional that checks practice_after_school.
pass
Your completed code should print four different recommendations: packed lunch with practice, packed lunch without practice, buying lunch with practice, and buying lunch without practice. Change both values to test all four outcomes. Then write the same structure in JavaScript and College Board pseudocode and explain which condition is checked first.
Homework Hack: High School Daily Routine Dashboard
Write your own Python nested conditional from scratch to guide a student through an ordinary school day. The starter runner gives you only the student-life data; you must design the complete decision structure yourself. Your dashboard should combine account access, homework, meals, and sports practice.
Your finished program must:
- store at least four student-life values, including login status, homework status, a meal decision, and sports-practice status,
- use an outer account
if/elsebefore checking the student’s schedule, - contain at least two nested decisions,
- produce at least four clear recommendations,
- test inputs that reach every meaningful outcome,
- include 2–3 sentences explaining why the inner checks depend on the outer decisions.
Homework Hack - Build a high school daily routine dashboard
student = {
"logged_in": True,
"homework_finished": False,
"lunch_packed": True,
"practice_today": True
}
# Write your complete nested conditional below.
# Do not delete the starter data. Test several value combinations.
Code Runner Challenge
Homework Hack - Build a high school daily routine dashboard
View IPYNB Source
# CODE_RUNNER: Homework Hack - Build a high school daily routine dashboard
student = {
"logged_in": True,
"homework_finished": False,
"lunch_packed": True,
"practice_today": True
}
# Write your complete nested conditional below.
# Do not delete the starter data. Test several value combinations.
Do not paste Markdown code fences.