3.07 Nested Conditionals HW
Popcorn Hack 1: After-School Homework Plan
1. Message that prints: Finish homework
2. Path followed: the outer if is_weekday: branch is taken because is_weekday is True. Inside it, homework_finished is False, so the inner else runs.
3. Reaching the other two outcomes:
is_weekday |
homework_finished |
Message |
|---|---|---|
True |
True |
Free time |
True |
False |
Finish homework |
False |
either | Weekend plan |
The runner tests all three paths.
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
def after_school_plan(is_weekday, homework_finished):
if is_weekday:
if homework_finished:
return "Free time"
else:
return "Finish homework"
else:
return "Weekend plan"
print("Original values:", after_school_plan(is_weekday, homework_finished))
print()
print("Testing every path:")
for weekday, finished in [(True, True), (True, False), (False, False)]:
print(" is_weekday =", weekday, "| homework_finished =", finished, "->", after_school_plan(weekday, finished))
Popcorn Hack 2: Repair the Student Account Check
Why the original code is not nested: if has_permission: starts at the left edge, at the same level as if logged_in:. That makes it a separate decision, so it runs even when the student is not logged in.
The fix: the permission check is indented inside the logged_in branch, and an outer else prints Log in first. Now the permission check only happens after a successful login.
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
def account_check(logged_in, has_permission):
if logged_in:
print(" Account found")
if has_permission:
print(" Page opened")
else:
print(" Permission needed")
else:
print(" Log in first")
print("Original values:")
account_check(logged_in, has_permission)
print()
print("Testing all three messages:")
for logged, permission in [(True, True), (True, False), (False, True)]:
print("logged_in =", logged, "| has_permission =", permission)
account_check(logged, permission)
Popcorn Hack 3: Lunch and Sports Practice
The outer decision checks lunch_packed. Each branch has its own nested check for practice_after_school, which gives four recommendations. The runner tests all four.
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
def lunch_plan(lunch_packed, practice_after_school):
if lunch_packed:
if practice_after_school:
return "Eat your packed lunch and bring an extra snack for practice"
else:
return "Eat your packed lunch"
else:
if practice_after_school:
return "Buy lunch and grab an extra snack before practice"
else:
return "Buy lunch at school"
print("Original values:", lunch_plan(lunch_packed, practice_after_school))
print()
print("Testing all four paths:")
for packed in [True, False]:
for practice in [True, False]:
print(" lunch_packed =", packed, "| practice_after_school =", practice, "->", lunch_plan(packed, practice))
The same structure in JavaScript:
Code Runner Challenge
Popcorn Hack 3 - Same lunch and practice plan in JavaScript
View IPYNB Source
%%js
// CODE_RUNNER: Popcorn Hack 3 - Same lunch and practice plan in JavaScript
const lunchPacked = true;
const practiceAfterSchool = true;
if (lunchPacked) {
if (practiceAfterSchool) {
console.log("Eat your packed lunch and bring an extra snack for practice");
} else {
console.log("Eat your packed lunch");
}
} else {
if (practiceAfterSchool) {
console.log("Buy lunch and grab an extra snack before practice");
} else {
console.log("Buy lunch at school");
}
}
The same structure in College Board pseudocode:
Code Runner Challenge
Homework Hack - Build a high school daily routine dashboard
View IPYNB Source
IF (lunchPacked)
{
IF (practiceAfterSchool)
{
DISPLAY("Eat your packed lunch and bring an extra snack for practice")
}
ELSE
{
DISPLAY("Eat your packed lunch")
}
}
ELSE
{
IF (practiceAfterSchool)
{
DISPLAY("Buy lunch and grab an extra snack before practice")
}
ELSE
{
DISPLAY("Buy lunch at school")
}
}
Which condition is checked first: lunch_packed is always checked first, because it is the outer condition. practice_after_school is checked second, inside whichever branch the lunch check picked. All three versions follow the same order.
Homework Hack: High School Daily Routine Dashboard
The dashboard checks the student’s account first. If the student is not logged in, nothing else is checked. If they are, nested decisions plan lunch around practice, then plan the evening around practice and homework.
| Logged in | Practice today | Lunch packed | Homework finished | Recommendations |
|---|---|---|---|---|
| No | any | any | any | Log in to see today’s plan |
| Yes | Yes | Yes / No | Yes / No | Packed or bought lunch plus a practice snack; after practice: free time or homework |
| Yes | No | Yes / No | Yes / No | Packed or bought lunch; after school: free time or homework before dinner |
That makes 9 outcomes: 1 logged-out path plus 8 logged-in combinations. The runner tests every one.
Why the inner checks depend on the outer decisions: the schedule should only be shown to a student who has logged in, so every schedule check is nested inside the account check. The lunch and evening advice changes based on whether there is practice, so those checks are nested inside the practice decision. If they were separate if statements, the dashboard could give practice advice on a day with no practice, or show a plan to someone who is not logged in.
# CODE_RUNNER: Homework Hack - Build a high school daily routine dashboard
student = {
"logged_in": True,
"homework_finished": False,
"lunch_packed": True,
"practice_today": True
}
# The account check is the outer decision, so no schedule advice is shown
# until the student logs in. The lunch and evening checks are nested inside
# the practice decision because practice changes what the student needs.
def daily_routine(student):
if student["logged_in"]:
print(" Welcome back! Here is today's plan:")
if student["practice_today"]:
if student["lunch_packed"]:
print(" Lunch: eat your packed lunch and save a snack for practice")
else:
print(" Lunch: buy lunch and grab an extra snack for practice")
if student["homework_finished"]:
print(" After practice: dinner, then free time")
else:
print(" After practice: dinner, then finish your homework")
else:
if student["lunch_packed"]:
print(" Lunch: eat your packed lunch")
else:
print(" Lunch: buy lunch at the cafeteria")
if student["homework_finished"]:
print(" After school: free time before dinner")
else:
print(" After school: finish homework before dinner")
else:
print(" Log in to see today's plan")
print("Starter student:")
daily_routine(student)
# Test every meaningful outcome: logged out, plus all 8 logged-in combinations.
print()
print("Test: logged out")
daily_routine({"logged_in": False, "homework_finished": True, "lunch_packed": True, "practice_today": True})
for practice in [True, False]:
for packed in [True, False]:
for finished in [True, False]:
print()
print("Test: practice_today =", practice, "| lunch_packed =", packed, "| homework_finished =", finished)
daily_routine({"logged_in": True, "homework_finished": finished,
"lunch_packed": packed, "practice_today": practice})