3.07 Nested Conditionals HW
Popcorn Hack 1: After-School Homework Plan
Answers to the Questions
-
Message printed: “Finish homework”
-
Outer and Inner branches followed:
Outer branch: is_weekday is True ($\to$ executes the outer if block)
Inner branch: homework_finished is False ($\to$ executes the inner else block)
- Values needed to reach each outcome: “Free time”: is_weekday = True, homework_finished = True “Finish homework”: is_weekday = True, homework_finished = False “Weekend plan”: is_weekday = False (homework_finished can be True or False)
Popcorn Hack 2: Repair the Student Account Check
The original code was not nested because line 7 (if has_permission:) started at the same indentation level as line 5 (if logged_in:). This meant Python treated them as two completely independent if statements. The permission check ran whether logged_in was True or False
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")
Popcorn Hack 3: Lunch and Sports 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("Packed lunch with practice: Eat packed lunch and bring an extra snack.")
else:
print("Packed lunch without practice: Just eat your packed lunch.")
else:
if practice_after_school:
print("Buying lunch with practice: Buy lunch at cafeteria and buy an extra afternoon snack.")
else:
print("Buying lunch without practice: Just buy cafeteria lunch.")
Popcorn Hack 3: Translations & Logic Explanation
JavaScript Translation
let lunchPacked = true;
let practiceAfterSchool = true;
if (lunchPacked) {
if (practiceAfterSchool) {
console.log("Packed lunch with practice: Eat packed lunch and bring an extra snack.");
} else {
console.log("Packed lunch without practice: Just eat your packed lunch.");
}
} else {
if (practiceAfterSchool) {
console.log("Buying lunch with practice: Buy lunch at cafeteria and buy an extra afternoon snack.");
} else {
console.log("Buying lunch without practice: Just buy cafeteria lunch.");
}
}
AP College Board Pseudocode Translation
IF (lunchPacked) {
IF (practiceAfterSchool) {
DISPLAY("Packed lunch with practice: Eat packed lunch and bring an extra snack.")
} ELSE {
DISPLAY("Packed lunch without practice: Just eat your packed lunch.")
}
} ELSE {
IF (practiceAfterSchool) {
DISPLAY("Buying lunch with practice: Buy lunch at cafeteria and buy an extra afternoon snack.")
} ELSE {
DISPLAY("Buying lunch without practice: Just buy cafeteria lunch.")
}
}
The outer condition (lunch_packed) is evaluated first. Python must determine whether the student brought lunch or needs to buy lunch before moving into the corresponding block to evaluate practice_after_school.
Homework Hack: High School Daily Routine Dashboard
Code Runner Challenge
Homework Hack - Build a high school daily routine dashboard
View IPYNB Source
# CODE_RUNNER: Homework Hack - Build a high school daily routine dashboard
student = {
"logged_in": True,
"homework_finished": False,
"lunch_packed": True,
"practice_today": True
}
# Nested Conditional Dashboard Logic
if student["logged_in"]:
print("Welcome to your Student Dashboard!")
# Outer Check 1: Homework & Free Time Schedule
if student["homework_finished"]:
print("-> Schedule: Homework complete! You have free time after school.")
else:
print("-> Schedule: Finish homework before relaxation time.")
# Outer Check 2: Meals & Sports Practice
if student["lunch_packed"]:
if student["practice_today"]:
print("-> Action: Pack extra snacks for practice after eating your lunch.")
else:
print("-> Action: You're all set with your packed lunch.")
else:
if student["practice_today"]:
print("-> Action: Buy cafeteria lunch AND get an extra energy bar for practice.")
else:
print("-> Action: Buy cafeteria lunch.")
else:
print("Access Denied: Please log in first to view your daily routine.")
The inner checks (homework_finished, lunch_packed, practice_today) depend directly on the outer decision (logged_in) because student schedule details and personal recommendations contain private data. If a user is not logged in, evaluating or displaying schedule details is useless and creates a security risk. By placing schedule decisions inside if student[“logged_in”]:, the system guarantees that personal instructions are processed only after identity is verified.