3.05 Nested Conditionals HW
Popcorn Hack 1: After-School Homework Plan (Beginner)
- Message that will print initially:
"Finish homework" - Outer and inner branches followed:
- Outer branch:
if is_weekday:(evaluates toTruebecauseis_weekday = True) - Inner branch:
else:inside theis_weekdayblock (chosen becausehomework_finished = False, soif homework_finished:is skipped)
- Outer branch:
- Testing all three outcomes: The code cell below runs the original values first, then changes
is_weekdayandhomework_finishedto test all three possible paths ("Finish homework","Free time", and"Weekend plan").
# Popcorn Hack 1 - Default run
is_weekday = True
homework_finished = False
print("--- Original Values (is_weekday=True, homework_finished=False) ---")
if is_weekday:
if homework_finished:
print("Free time")
else:
print("Finish homework")
else:
print("Weekend plan")
# Testing the other two outcomes
print("\n--- Path 2 (is_weekday=True, homework_finished=True) ---")
is_weekday = True
homework_finished = True
if is_weekday:
if homework_finished:
print("Free time")
else:
print("Finish homework")
else:
print("Weekend plan")
print("\n--- Path 3 (is_weekday=False, homework_finished=False) ---")
is_weekday = False
homework_finished = False
if is_weekday:
if homework_finished:
print("Free time")
else:
print("Finish homework")
else:
print("Weekend plan")
--- Original Values (is_weekday=True, homework_finished=False) ---
Finish homework
--- Path 2 (is_weekday=True, homework_finished=True) ---
Free time
--- Path 3 (is_weekday=False, homework_finished=False) ---
Weekend plan
Popcorn Hack 2: Repair the Student Account Check (Intermediate)
- Why the original code is not nested: In the starter code,
if has_permission:is at the exact same outer indentation level asif logged_in:. Because it is not indented insideif logged_in:, Python treats them as two separate, sequentialifstatements and checkshas_permissioneven whenlogged_inisFalse. - How it was repaired: Indented the entire
if has_permission: / else:block inside theif logged_in:branch and added an outerelse:branch that prints"Log in first".
# Popcorn Hack 2 - Repaired student account check
logged_in = True
has_permission = False
print("--- Test 1: Logged in, No permission (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")
print("\n--- Test 2: Logged in, Has permission (logged_in=True, has_permission=True) ---")
logged_in = True
has_permission = True
if logged_in:
print("Account found")
if has_permission:
print("Page opened")
else:
print("Permission needed")
else:
print("Log in first")
print("\n--- Test 3: Not logged in (logged_in=False, has_permission=True) ---")
logged_in = False
has_permission = True
if logged_in:
print("Account found")
if has_permission:
print("Page opened")
else:
print("Permission needed")
else:
print("Log in first")
--- Test 1: Logged in, No permission (logged_in=True, has_permission=False) ---
Account found
Permission needed
--- Test 2: Logged in, Has permission (logged_in=True, has_permission=True) ---
Account found
Page opened
--- Test 3: Not logged in (logged_in=False, has_permission=True) ---
Log in first
Popcorn Hack 3: Lunch and Sports Practice (Advanced)
1. JavaScript Translation
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 practice snack needed today.");
}
} else {
if (practiceAfterSchool) {
console.log("Buy lunch in the cafeteria and grab an extra snack for practice.");
} else {
console.log("Buy lunch in the cafeteria; no extra practice snack needed today.");
}
}
2. College Board Pseudocode Translation
IF (lunchPacked)
{
IF (practiceAfterSchool)
{
DISPLAY("Eat your packed lunch and bring an extra snack for practice.")
}
ELSE
{
DISPLAY("Eat your packed lunch; no extra practice snack needed today.")
}
}
ELSE
{
IF (practiceAfterSchool)
{
DISPLAY("Buy lunch in the cafeteria and grab an extra snack for practice.")
}
ELSE
{
DISPLAY("Buy lunch in the cafeteria; no extra practice snack needed today.")
}
}
3. Execution Order Explanation
Python checks the outer condition (if lunch_packed:) first to determine whether the student brought lunch from home or needs to buy lunch at school. Only after selecting either the if lunch_packed: branch or the outer else: branch does Python evaluate the inner condition (if practice_after_school:) inside that chosen path to decide if an extra snack is needed for practice.
# Popcorn Hack 3 - Test the lunch and sports-practice plan
lunch_packed = True
practice_after_school = True
print("--- Default Run ---")
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 practice snack needed today.")
else:
if practice_after_school:
print("Buy lunch in the cafeteria and grab an extra snack for practice.")
else:
print("Buy lunch in the cafeteria; no extra practice snack needed today.")
print("\n--- Testing All 4 Outcomes ---")
test_cases = [
(True, True),
(True, False),
(False, True),
(False, False)
]
for packed, practice in test_cases:
print(f"lunch_packed={packed}, practice_after_school={practice} -> ", end="")
if packed:
if practice:
print("Eat your packed lunch and bring an extra snack for practice.")
else:
print("Eat your packed lunch; no extra practice snack needed today.")
else:
if practice:
print("Buy lunch in the cafeteria and grab an extra snack for practice.")
else:
print("Buy lunch in the cafeteria; no extra practice snack needed today.")
--- Default Run ---
Eat your packed lunch and bring an extra snack for practice.
--- Testing All 4 Outcomes ---
lunch_packed=True, practice_after_school=True -> Eat your packed lunch and bring an extra snack for practice.
lunch_packed=True, practice_after_school=False -> Eat your packed lunch; no extra practice snack needed today.
lunch_packed=False, practice_after_school=True -> Buy lunch in the cafeteria and grab an extra snack for practice.
lunch_packed=False, practice_after_school=False -> Buy lunch in the cafeteria; no extra practice snack needed today.
Homework Hack: High School Daily Routine Dashboard
Why the Inner Checks Depend on the Outer Decisions
The outer if student["logged_in"]: condition must be checked first because a student’s personal homework, meal, and sports schedule cannot be loaded or shown until their account login is verified. Once inside the logged-in branch, the inner checks for practice_today and homework_finished depend on the outer meal and schedule branches so that afternoon snack and study recommendations are tailored to what the student actually has planned that day.
# Homework Hack - Build a high school daily routine dashboard
student = {
"logged_in": True,
"homework_finished": False,
"lunch_packed": True,
"practice_today": True
}
def run_student_dashboard(data):
if data["logged_in"]:
print("Welcome to your Student Life Dashboard!")
# Nested Decision 1: Lunch and Sports Practice
if data["lunch_packed"]:
if data["practice_today"]:
print("- Meal/Sports Plan: Eat your packed lunch and save your extra snack for after-school practice.")
else:
print("- Meal/Sports Plan: Eat your packed lunch; no practice today, so head straight home after school.")
else:
if data["practice_today"]:
print("- Meal/Sports Plan: Buy lunch at the cafeteria and pick up an energy bar for practice.")
else:
print("- Meal/Sports Plan: Buy lunch at the cafeteria; no practice snack needed today.")
# Nested Decision 2: Homework and Evening Schedule
if data["practice_today"]:
if data["homework_finished"]:
print("- Evening Plan: Homework is already done! Focus on practice, then enjoy a relaxing dinner.")
else:
print("- Evening Plan: Finish your homework right after practice before having free time.")
else:
if data["homework_finished"]:
print("- Evening Plan: No practice and homework is done! You have free time all afternoon.")
else:
print("- Evening Plan: No practice today—use your afternoon to finish homework before dinner.")
else:
print("Access Denied: Please log in to your student account first to view your daily schedule.")
print("=== Starter Data Run ===")
run_student_dashboard(student)
print("\n=== Testing All Meaningful Outcomes ===")
test_profiles = [
{"name": "1. Not Logged In", "logged_in": False, "homework_finished": True, "lunch_packed": True, "practice_today": True},
{"name": "2. Logged In | Packed Lunch | Practice | HW Not Done", "logged_in": True, "homework_finished": False, "lunch_packed": True, "practice_today": True},
{"name": "3. Logged In | Packed Lunch | No Practice | HW Done", "logged_in": True, "homework_finished": True, "lunch_packed": True, "practice_today": False},
{"name": "4. Logged In | Buy Lunch | Practice | HW Done", "logged_in": True, "homework_finished": True, "lunch_packed": False, "practice_today": True},
{"name": "5. Logged In | Buy Lunch | No Practice | HW Not Done", "logged_in": True, "homework_finished": False, "lunch_packed": False, "practice_today": False}
]
for profile in test_profiles:
print(f"\n[{profile['name']}]")
run_student_dashboard(profile)
=== Starter Data Run ===
Welcome to your Student Life Dashboard!
- Meal/Sports Plan: Eat your packed lunch and save your extra snack for after-school practice.
- Evening Plan: Finish your homework right after practice before having free time.
=== Testing All Meaningful Outcomes ===
[1. Not Logged In]
Access Denied: Please log in to your student account first to view your daily schedule.
[2. Logged In | Packed Lunch | Practice | HW Not Done]
Welcome to your Student Life Dashboard!
- Meal/Sports Plan: Eat your packed lunch and save your extra snack for after-school practice.
- Evening Plan: Finish your homework right after practice before having free time.
[3. Logged In | Packed Lunch | No Practice | HW Done]
Welcome to your Student Life Dashboard!
- Meal/Sports Plan: Eat your packed lunch; no practice today, so head straight home after school.
- Evening Plan: No practice and homework is done! You have free time all afternoon.
[4. Logged In | Buy Lunch | Practice | HW Done]
Welcome to your Student Life Dashboard!
- Meal/Sports Plan: Buy lunch at the cafeteria and pick up an energy bar for practice.
- Evening Plan: Homework is already done! Focus on practice, then enjoy a relaxing dinner.
[5. Logged In | Buy Lunch | No Practice | HW Not Done]
Welcome to your Student Life Dashboard!
- Meal/Sports Plan: Buy lunch at the cafeteria; no practice snack needed today.
- Evening Plan: No practice today—use your afternoon to finish homework before dinner.