3.7 Nested Conditionals HW
Popcorn Hack 1: After-School Homework Plan (Beginner)
Trace: is_weekday is True, so the outer branch enters the weekday path. Then homework_finished is False, so the inner else runs.
Prediction: Finish homework
Outer branch: if is_weekday (True) Inner branch: else under if homework_finished (False)
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")
Actual output: Finish homework
Reaching the other two outcomes: is_weekday = True, homework_finished = True gives Free time. is_weekday = False (homework value does not matter) gives Weekend plan. The next cell tests all three paths.
Code Runner Challenge
Popcorn Hack 1 - All three paths
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - All three paths
tests = [(True, False), (True, True), (False, False)]
for is_weekday, homework_finished in tests:
if is_weekday:
if homework_finished:
print("Free time")
else:
print("Finish homework")
else:
print("Weekend plan")
Actual output: Finish homework, Free time, Weekend plan.
Popcorn Hack 2: Repair the Student Account Check (Intermediate)
Why the original is not nested: The second if has_permission: sits at the same indentation as if logged_in:, so it is a separate, independent decision. Permission gets checked even when the student is not logged in. Indentation is what makes a decision “belong” to another one.
Fixed: the permission check is indented inside the logged-in branch, and an outer else prints Log in first.
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")
else:
print("Log in first")
Actual output: Account found, then Permission needed.
Test all three messages
Code Runner Challenge
Popcorn Hack 2 - Test all three outcomes
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Test all three outcomes
tests = [(True, True), (True, False), (False, False)]
for logged_in, has_permission in tests:
if logged_in:
print("Account found")
if has_permission:
print("Page opened")
else:
print("Permission needed")
else:
print("Log in first")
print("---")
Actual output: Account found / Page opened, then Account found / Permission needed, then Log in first. Three possible outcomes, and only one of the two permission messages can ever appear after a successful login.
Popcorn Hack 3: Lunch and Sports Practice (Advanced)
lunch_packed is checked first (outer). practice_after_school is checked inside each branch (inner), so the inner decision only happens after the lunch decision picks a path.
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
tests = [(True, True), (True, False), (False, True), (False, False)]
for lunch_packed, practice_after_school in tests:
if lunch_packed:
if practice_after_school:
print("Eat your packed lunch and bring an extra snack for practice")
else:
print("Eat your packed lunch, no extra snack needed")
else:
if practice_after_school:
print("Buy lunch and buy an extra snack for practice")
else:
print("Buy lunch, no extra snack needed")
Actual output: four different recommendations, one per combination (packed + practice, packed + no practice, buy + practice, buy + no practice).
Same structure in JavaScript:
Code Runner Challenge
Homework Hack - Build a high school daily routine dashboard
View IPYNB Source
if (lunchPacked) {
if (practiceAfterSchool) {
console.log("Eat your packed lunch and bring an extra snack for practice");
} else {
console.log("Eat your packed lunch, no extra snack needed");
}
} else {
if (practiceAfterSchool) {
console.log("Buy lunch and buy an extra snack for practice");
} else {
console.log("Buy lunch, no extra snack needed");
}
}
Same structure in College Board pseudocode:
IF (lunchPacked)
{
IF (practiceAfterSchool)
{
DISPLAY("Eat your packed lunch and bring an extra snack for practice")
}
ELSE
{
DISPLAY("Eat your packed lunch, no extra snack needed")
}
}
ELSE
{
IF (practiceAfterSchool)
{
DISPLAY("Buy lunch and buy an extra snack for practice")
}
ELSE
{
DISPLAY("Buy lunch, no extra snack needed")
}
}
Which condition is checked first: lunchPacked / lunch_packed. Only after it chooses the packed or buy path does the program ask about practice.
Homework Hack: High School Daily Routine Dashboard
Outer decision: is the student logged in? Inside that: is homework finished? Inside that: is there practice today, and (when there is) is lunch packed? The starter student dictionary is kept and is the first test case, followed by cases that reach every other outcome.
# CODE_RUNNER: Homework Hack - Build a high school daily routine dashboard
student = {
"logged_in": True,
"homework_finished": False,
"lunch_packed": True,
"practice_today": True
}
# Test cases: the starter student first, then one case for every other outcome
test_students = [
student,
{"logged_in": False, "homework_finished": False, "lunch_packed": False, "practice_today": False},
{"logged_in": True, "homework_finished": False, "lunch_packed": True, "practice_today": False},
{"logged_in": True, "homework_finished": True, "lunch_packed": True, "practice_today": True},
{"logged_in": True, "homework_finished": True, "lunch_packed": False, "practice_today": True},
{"logged_in": True, "homework_finished": True, "lunch_packed": True, "practice_today": False},
]
for s in test_students:
# Outer decision: nothing else is checked until the account is logged in
if s["logged_in"]:
# Inner decision 1: homework comes before free time or practice
if s["homework_finished"]:
# Inner decision 2: practice only matters once homework is done
if s["practice_today"]:
# Inner decision 3: the snack plan depends on the practice
if s["lunch_packed"]:
print("Homework done. Head to practice and pack a snack from your lunch.")
else:
print("Homework done. Buy a snack before practice.")
else:
print("Homework done and no practice. Enjoy free time and an early dinner.")
else:
if s["practice_today"]:
print("Finish homework at lunch so you can go to practice.")
else:
print("Finish homework after school, then free time.")
else:
print("Log in first to see your dashboard.")
Actual output (in order): Finish homework at lunch so you can go to practice. / Log in first to see your dashboard. / Finish homework after school, then free time. / Homework done. Head to practice and pack a snack from your lunch. / Homework done. Buy a snack before practice. / Homework done and no practice. Enjoy free time and an early dinner.
Why the inner checks depend on the outer decisions: The program can only talk about homework, meals, or practice once the account check says the student is logged in, so those checks live inside the logged-in branch. Practice is only worth planning a snack for after homework is finished, and the snack plan only matters when there is practice, so each deeper check depends on the answer above it. That is different from one long and condition because each path leads to its own different recommendation.
Six outcomes tested (one not-logged-in, two homework-unfinished, three homework-finished), reaching every meaningful path.