Python 3.07 - Nested Conditionals HW
Python 3.07 Nested Conditionals
Completed all three Popcorn Hacks and the daily-routine dashboard homework.
Popcorn Hack 1: After-School Homework Plan
Prediction for the starter values: Finish homework. All three outcomes are tested below.
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
for is_weekday, homework_finished in [(True, True), (True, False), (False, False)]:
if is_weekday:
if homework_finished:
print("Free time")
else:
print("Finish homework")
else:
print("Weekend plan")
Popcorn Hack 2: Repair the Student Account Check
The permission check wasn’t indented inside the login check, so it ran even when logged out. Now it’s nested, with an 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
for logged_in, has_permission in [(True, True), (True, False), (False, False)]:
if logged_in:
if has_permission:
print("Page opened")
else:
print("Permission needed")
else:
print("Log in first")
Popcorn Hack 3: Lunch and Sports Practice
The lunch check comes first, and the practice check is nested inside it. All four combinations are tested.
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
for lunch_packed in [True, False]:
for practice_after_school in [True, False]:
if lunch_packed:
if practice_after_school:
print("Bring packed lunch and an extra practice snack")
else:
print("Bring packed lunch")
else:
if practice_after_school:
print("Buy lunch and get an extra snack for practice")
else:
print("Buy lunch")
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");
}
}
College Board pseudocode
Code Runner Challenge
Homework tests - exercise meaningful dashboard outcomes
View IPYNB Source
IF (lunch_packed)
{
IF (practice_after_school)
{ DISPLAY("Bring packed lunch and an extra practice snack") }
ELSE
{ DISPLAY("Bring packed lunch") }
}
ELSE
{
IF (practice_after_school)
{ DISPLAY("Buy lunch and get an extra snack for practice") }
ELSE
{ DISPLAY("Buy lunch") }
}
Homework Hack: Daily Routine Dashboard
Login is the outer check. Homework, lunch and practice are only checked after the student is logged in.
# CODE_RUNNER: Homework Hack - high school daily routine dashboard
logged_in = True
homework_finished = False
lunch_packed = True
practice_today = True
if logged_in:
if homework_finished:
print("Homework complete - free time after school")
else:
print("Finish homework before free time")
if lunch_packed:
if practice_today:
print("Bring packed lunch and an extra snack for practice")
else:
print("Bring packed lunch")
else:
if practice_today:
print("Buy lunch and get an extra snack for practice")
else:
print("Buy lunch")
else:
print("Log in first")
Homework Tests
Tested logged out, unfinished homework with practice, finished homework without practice, and buying lunch.
# CODE_RUNNER: Homework tests - exercise meaningful dashboard outcomes
tests = [
(False, False, False, False),
(True, False, True, True),
(True, True, True, False),
(True, True, False, True),
]
for logged_in, homework_finished, lunch_packed, practice_today in tests:
print("Test:", logged_in, homework_finished, lunch_packed, practice_today)
if logged_in:
if homework_finished:
print(" Homework complete")
else:
print(" Finish homework")
if lunch_packed:
if practice_today:
print(" Packed lunch + practice snack")
else:
print(" Packed lunch")
else:
if practice_today:
print(" Buy lunch + practice snack")
else:
print(" Buy lunch")
else:
print(" Log in first")
Explanation
The other checks are nested inside the login check because a student shouldn’t see their schedule until they’re logged in. The practice check is nested inside the lunch check because the right lunch advice depends on both.