3.07 Nested Conditionals HW
3.07 Nested Conditionals: Homework Notebook
Lesson: 3.07 Nested Conditionals - Student Life Contents: Popcorn Hacks 1-3 (traced, repaired, completed, and tested), JavaScript + College Board pseudocode translation, Homework Hack (High School Daily Routine Dashboard), explanation of the nesting.
Popcorn Hack 1: After-School Homework Plan (Beginner)
1. Message that will print: Finish homework.
2. Branches followed: the outer branch is the if is_weekday: branch (it’s True); then the inner branch is the else of if homework_finished: (homework isn’t finished).
3. The other two outcomes: Free time needs is_weekday = True and homework_finished = True. Weekend plan needs is_weekday = False (the inner check is skipped entirely).
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")
# Testing every path
print()
for is_weekday, homework_finished in [(True, False), (True, True), (False, False), (False, True)]:
if is_weekday:
if homework_finished:
result = "Free time"
else:
result = "Finish homework"
else:
result = "Weekend plan"
print(f"is_weekday={is_weekday!s:<5} homework_finished={homework_finished!s:<5} -> {result}")
Notice that when is_weekday is False, the value of homework_finished doesn’t matter, because the inner if belongs to the first branch and is never reached.
Popcorn Hack 2: Repair the Student Account Check (Intermediate)
1. Why the original isn’t nested: the second if has_permission: is at the same indentation level as if logged_in:. Python treats them as two separate, back-to-back decisions, so the permission check runs even when the student is not logged in.
2-3. Fix: indent the permission check inside the logged-in branch and add an outer else that prints Log in first.
4. Test all three messages (below).
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: # now INSIDE the logged-in branch
print("Page opened")
else:
print("Permission needed")
else:
print("Log in first")
# Test every possible path
print()
for logged_in, has_permission in [(True, True), (True, False), (False, True), (False, False)]:
print(f"logged_in={logged_in!s:<5} has_permission={has_permission!s:<5} ->", end=" ")
if logged_in:
print("Account found, then", end=" ")
if has_permission:
print("Page opened")
else:
print("Permission needed")
else:
print("Log in first")
Three messages reached: Page opened (logged in + permission), Permission needed (logged in, no permission), and Log in first (not logged in, where permission is never checked).
Popcorn Hack 3: Lunch and Sports Practice (Advanced)
Four recommendations: packed lunch + practice, packed lunch + no practice, buy lunch + practice, buy lunch + no practice.
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:
if practice_after_school:
print("Eat your packed lunch and bring an extra snack for practice")
else:
print("Eat your packed lunch, then head home: no extra snack needed")
else:
if practice_after_school:
print("Buy lunch at school and buy a snack for practice too")
else:
print("Buy lunch at school: no snack needed")
# Test all four outcomes by changing both values
print()
for lunch_packed in (True, False):
for practice_after_school in (True, False):
print(f"lunch_packed={lunch_packed!s:<5} practice_after_school={practice_after_school!s:<5} ->", end=" ")
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, then head home: no extra snack needed")
else:
if practice_after_school:
print("Buy lunch at school and buy a snack for practice too")
else:
print("Buy lunch at school: no snack needed")
Same structure in JavaScript
Code Runner Challenge
Homework Hack - Build a high school daily routine dashboard
View IPYNB Source
let lunchPacked = true;
let 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, then head home: no extra snack needed");
}
} else {
if (practiceAfterSchool) {
console.log("Buy lunch at school and buy a snack for practice too");
} else {
console.log("Buy lunch at school: no 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, then head home: no extra snack needed")
}
}
ELSE
{
IF (practiceAfterSchool)
{
DISPLAY("Buy lunch at school and buy a snack for practice too")
}
ELSE
{
DISPLAY("Buy lunch at school: no snack needed")
}
}
Which condition is checked first? lunch_packed / lunchPacked, the outer condition, is always checked first. Only after it picks a branch does the program reach the inner practice_after_school check, and each branch has its own copy of that inner decision.
Homework Hack: High School Daily Routine Dashboard
Combines accounts, homework, meals, and sports practice in one nested conditional. The outer if / else checks the account first; everything about the student’s schedule is nested inside the logged-in branch.
Starter data is kept at the top; the dashboard logic is in show_dashboard, and the test loop at the bottom runs every meaningful combination.
# 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.
def show_dashboard(student):
# OUTER decision: account access comes first
if student["logged_in"]:
# SECOND level: homework decides which kind of day this is
if student["homework_finished"]:
# THIRD level: sports practice
if student["practice_today"]:
# FOURTH level: meal plan for a practice day
if student["lunch_packed"]:
print("Homework done + practice: eat your packed lunch, go to practice, then enjoy dinner and free time")
else:
print("Homework done + practice: buy lunch AND a snack, go to practice, then dinner and free time")
else:
if student["lunch_packed"]:
print("Homework done, no practice: eat your packed lunch and enjoy free time after school")
else:
print("Homework done, no practice: buy lunch, then take a free-time break")
else:
if student["practice_today"]:
if student["lunch_packed"]:
print("Homework unfinished + practice: use packed-lunch time to start homework, finish it after practice")
else:
print("Homework unfinished + practice: buy a quick lunch, start homework at lunch, finish after practice")
else:
print("Homework unfinished, no practice: finish homework right after school, then dinner")
else:
print("Log in first")
# Run the starter student exactly as given
print("Starter student:", student)
show_dashboard(student)
# Test every meaningful combination of the four values
print()
print("=== All test combinations ===")
tests = []
for logged_in in (True, False):
for homework_finished in (True, False):
for lunch_packed in (True, False):
for practice_today in (True, False):
if not logged_in and not (homework_finished and lunch_packed and practice_today):
continue # when logged out nothing else matters, so test it once
tests.append({
"logged_in": logged_in,
"homework_finished": homework_finished,
"lunch_packed": lunch_packed,
"practice_today": practice_today
})
for number, test in enumerate(tests, start=1):
flags = "".join("T" if test[k] else "F" for k in ["logged_in", "homework_finished", "lunch_packed", "practice_today"])
print(f"Test {number:>2} [{flags}] ->", end=" ")
show_dashboard(test)
Why the inner checks depend on the outer decisions
The account check is the outer decision because nothing about a student’s homework, lunch, or practice should be shown until the program knows who the student is: if logged_in is False, every inner check is skipped and only Log in first prints. Likewise, the practice and meal decisions are nested inside the homework branch because the best recommendation changes with it: a student with finished homework gets a relaxing plan, while one with unfinished homework needs a plan that fits studying around practice and lunch. Each inner if therefore only makes sense after the outer branch has selected its path, which is why a nested conditional is the right structure rather than one long and expression.
Outcomes covered: 1 logged-out message + 7 logged-in recommendations (finished/unfinished homework x practice/no practice x packed/bought lunch where the plan differs), all exercised by the test loop.
Submission Notes
Lesson: 3.07 Nested Conditionals - Student Life
Popcorn Hacks 1-3: traced, repaired, completed, and tested all paths
Homework Hack: dashboard with outer account if/else, 3 levels of nesting inside it, 8 distinct messages (7 recommendations + Log in first)
Testing: all 8 logged-in combinations plus 1 logged-out test (every outcome printed above)
Submit only the Homework Hack code cell above in the Code Submission form (no Markdown fences).