Python 3.06 Conditionals

This notebook covers both Popcorn Hacks and the scam-message trainer Homework Hack.

Popcorn Hack 1: Fix the Silent Failure

With payment_request = 0, the original code printed nothing. That is a logic bug, not a Python error, because the false outcome had no answer.

Code Runner Challenge

3.6 Popcorn 1 - make both outcomes print

View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 1 - make both outcomes print

payment_request = 0

# 1 condition -> 2 outcomes -> 2 answers
# The else prevents the false case from silently falling off the end.
if payment_request > 100:
    print("This message is asking for a large amount of money.")
else:
    print("This message is not asking for a large amount of money.")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Fix the elif Order

In the broken version, risk_points >= 6 and risk_points >= 9 could never run because every score that satisfies them also satisfies the first risk_points >= 3 branch. The fix is to test the highest threshold first.

Code Runner Challenge

3.6 Popcorn 2 - put thresholds in reachable order

View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 2 - put thresholds in reachable order

risk_points = 9

# Highest-to-lowest ordering keeps every branch reachable.
# 3 thresholds -> 4 outcomes -> 4 answers
if risk_points >= 9:
    print("Do not reply. This is almost certainly a scam.")
elif risk_points >= 6:
    print("This looks risky. Check with someone you trust before replying.")
elif risk_points >= 3:
    print("Be a little careful, but this is probably fine.")
else:
    print("Nothing suspicious in this message.")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack: Scam-Message Trainer

Each signal adds independently to the score, then a highest-to-lowest if / elif / else chain gives every possible score an answer.

Code Runner Challenge

3.6 Homework - score a message, then advise on it

View IPYNB Source
# CODE_RUNNER: 3.6 Homework - score a message, then advise on it

def check_message(message, payment_request, known_sender):
    # Start from zero so each scam signal can contribute its own points.
    risk_points = 0

    # No else is needed for these scoring checks because a missing signal should simply add zero points.
    if "gift card" in message.lower():
        risk_points += 4

    # This signal is independent, so it can add points even when another signal already matched.
    if payment_request > 100:
        risk_points += 3

    # Again, no else is needed: a known contact contributes no extra risk points.
    if not known_sender:
        risk_points += 2

    # 3 thresholds -> 4 outcomes -> 4 answers
    # Highest thresholds come first so no more serious branch is hidden by an easier condition.
    if risk_points >= 7:
        recommendation = "Do not reply. This message has multiple strong scam signals."
    elif risk_points >= 4:
        recommendation = "This message looks risky. Verify it before responding."
    elif risk_points >= 1:
        recommendation = "Use caution and check the sender or request."
    else:
        recommendation = "No scam signals were detected by this trainer."

    print("Risk score:", risk_points)
    print(recommendation)
    return risk_points

# Test the exact starter message from the lesson: all three signals are present.
check_message("URGENT: send gift cards now to unlock your account", 500, False)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Reachability Proof

  • Score 9 / highest branch: gift-card message, amount 500, unknown contact → 4 + 3 + 2 = 9.
  • Score 7 / risky branch: gift-card message, amount 500, known contact → 4 + 3 = 7; to make the second branch distinct in testing below, use score 4 instead.
  • Score 4 / second branch: gift-card message, amount 0, known contact → 4.
  • Score 2 / caution branch: clean message, amount 0, unknown contact → 2.
  • Score 0 / safe branch: clean message, amount 0, known contact → 0.

The separate scoring if statements are correct without else because each one asks whether to add points; doing nothing is the intended behavior when a signal is absent.

Code Runner Challenge

3.6 Homework tests - prove every reporting branch is reachable

View IPYNB Source
# CODE_RUNNER: 3.6 Homework tests - prove every reporting branch is reachable

# Each case is chosen to hit a different reporting outcome, including both required extremes.
tests = [
    ("URGENT: send gift cards now", 500, False),   # score 9: all signals
    ("Could you purchase a gift card", 0, True),          # score 4: gift-card only
    ("Hello, just checking in", 0, False),                # score 2: unknown contact only
    ("Hey Dad, see you after school", 0, True)           # score 0: completely clean
]

# Running every test confirms no input falls through without output.
for message, amount, known in tests:
    print("---")
    check_message(message, amount, known)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...