Python 3.07 Nested Conditionals

This notebook covers all three Popcorn Hacks and the Homework Hack using nested decisions about homework, account access, meals, and sports practice.

Popcorn Hack 1: After-School Homework Plan

Prediction for the starter values: Finish homework. The outer if is_weekday branch is taken first, then the inner else runs because work_done is False.

The three outcomes are tested below: Free time, Finish homework, and Weekend plan.

Code Runner Challenge

Popcorn Hack 1 - test every after-school homework outcome

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - test every after-school homework outcome
# Each pair represents one meaningful path through the nested conditional.
scenarios = [
    (True, True),
    (True, False),
    (False, False)
]

for is_weekday, work_done in scenarios:
    # The weekday decision is checked first.
    if is_weekday:
        # This inner decision only runs on a weekday.
        if work_done:
            print("Free time")
        else:
            print("Finish homework")
    else:
        print("Weekend plan")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Repair the Student Account Check

The starter code was not nested because if access_allowed was aligned with if logged_in, so Python checked permission even when the student was not logged in. The fixed version places the permission check inside the login branch and adds the required outer else.

Code Runner Challenge

Popcorn Hack 2 - repair and test the account check

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - repair and test the account check
# These cases reach all three intended messages.
scenarios = [
    (True, True),
    (True, False),
    (False, False)
]

for logged_in, access_allowed in scenarios:
    # Permission is only relevant after the student is logged in.
    if logged_in:
        if access_allowed:
            print("Page opened")
        else:
            print("Permission needed")
    else:
        print("Log in first")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: Lunch and Sports Practice

The outer condition checks whether lunch is packed first. Only after that path is chosen does the program check whether practice is scheduled. All four combinations are tested below.

Code Runner Challenge

Popcorn Hack 3 - test all lunch and practice paths

View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - test all lunch and practice paths
# Four Boolean combinations cover every possible branch.
scenarios = [
    (True, True),
    (True, False),
    (False, True),
    (False, False)
]

for has_lunch, practice_later in scenarios:
    # First decide whether the student already has lunch.
    if has_lunch:
        # Then decide whether an extra practice snack is useful.
        if practice_later:
            print("Bring packed lunch and an extra practice snack")
        else:
            print("Bring packed lunch")
    else:
        # If lunch is not packed, choose what to buy based on practice plans.
        if practice_later:
            print("Buy lunch and get an extra snack for practice")
        else:
            print("Buy lunch")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3 Translation

JavaScript

Code Runner Challenge

Homework Hack - high school daily routine dashboard

View IPYNB Source
if (lunchPacked) {
  if (practiceAfterSchool) {
    console.log("Bring packed lunch and an extra practice snack");
  } else {
    console.log("Bring packed lunch");
  }
} else {
  if (practiceAfterSchool) {
    console.log("Buy lunch and get an extra snack for practice");
  } else {
    console.log("Buy lunch");
  }
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

College Board pseudocode

Code Runner Challenge

Homework tests - exercise meaningful dashboard outcomes

View IPYNB Source
IF (has_lunch)
{
  IF (practice_later)
  {
    DISPLAY("Bring packed lunch and an extra practice snack")
  }
  ELSE
  {
    DISPLAY("Bring packed lunch")
  }
}
ELSE
{
  IF (practice_later)
  {
    DISPLAY("Buy lunch and get an extra snack for practice")
  }
  ELSE
  {
    DISPLAY("Buy lunch")
  }
}
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

The lunch condition is checked first. The practice condition is checked only inside the lunch path that was selected.

Homework Hack: High School Daily Routine Dashboard

The dashboard uses login status as the outer decision before checking homework, lunch, and practice. It contains multiple nested decisions and produces clear recommendations for different student-life situations.

# CODE_RUNNER: Homework Hack - high school daily routine dashboard
# Starter student-life data from the lesson.
student = {
    "logged_in": True,
    "work_done": False,
    "has_lunch": True,
    "practice_today": True
}

# The account check comes first because schedule details should only be shown after login.
if student["logged_in"]:
    print("Account access granted")

    # Homework status decides whether the student is ready for after-school activities.
    if student["work_done"]:
        print("Homework complete — after-school time is available")
    else:
        print("Finish homework before free time")

    # Meal planning is checked separately because practice changes what the student should bring.
    if student["has_lunch"]:
        if student["practice_today"]:
            print("Bring packed lunch and an extra snack for practice")
        else:
            print("Bring packed lunch")
    else:
        if student["practice_today"]:
            print("Buy lunch and get an extra snack for practice")
        else:
            print("Buy lunch")

    # Practice creates one more schedule recommendation after homework and meal planning.
    if student["practice_today"]:
        if student["work_done"]:
            print("Go to practice after school")
        else:
            print("Plan homework time before practice")
    else:
        print("No practice today — use the extra time for schoolwork or rest")
else:
    print("Log in first before viewing the daily routine")

Homework Tests

The program should be tested with several combinations so every meaningful branch can run. Example cases include: logged out; logged in with homework unfinished and practice; logged in with homework finished and no practice; and both packed-lunch/buy-lunch paths.

# CODE_RUNNER: Homework tests - exercise meaningful dashboard outcomes
# Running several states verifies that the nested decisions reach the important account, meal, homework, and practice branches.
# Each dictionary changes the account, homework, meal, and practice state.
student_cases = [
    {"logged_in": False, "work_done": False, "has_lunch": False, "practice_today": False},
    {"logged_in": True, "work_done": False, "has_lunch": True, "practice_today": True},
    {"logged_in": True, "work_done": True, "has_lunch": True, "practice_today": False},
    {"logged_in": True, "work_done": True, "has_lunch": False, "practice_today": True},
    {"logged_in": True, "work_done": False, "has_lunch": False, "practice_today": False}
]

for student in student_cases:
    print("\nStudent state:", student)
    if student["logged_in"]:
        if student["work_done"]:
            print("Homework complete")
        else:
            print("Finish homework")

        if student["has_lunch"]:
            if student["practice_today"]:
                print("Packed lunch + practice snack")
            else:
                print("Packed lunch")
        else:
            if student["practice_today"]:
                print("Buy lunch + practice snack")
            else:
                print("Buy lunch")
    else:
        print("Log in first")

Explanation

The schedule checks depend on the account check because a student should not see personalized homework, meal, or practice recommendations until the program knows the account is accessible. The meal and practice checks are nested because the correct recommendation changes based on the earlier lunch decision, while the practice check refines that choice.