Popcorn Hack 1: The Silent Failure

What the original prints: nothing at all. amount_requested is 0, so 0 > 100 is False, and there is no else to run.

Error or bug? A bug. Python has no problem running the code, so there is no error message. The program just silently gives no answer for the “not a large amount” case.

Fix: added an else so both outcomes print, plus the count comment.

Code Runner Challenge

3.6 Popcorn 1 - Both outcomes now print

View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 1 - Both outcomes now print

amount_requested = 0

# 1 condition -> 2 outcomes -> 2 answers
if amount_requested > 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: The Chain in the Wrong Order

What the trainer tells someone with a score of 9: “Be a little careful, but this is probably fine.” That is dangerous advice for the worst possible score.

Branches that can never run: elif risk_score >= 6 and elif risk_score >= 9. Any score that is 6 or more, or 9 or more, is also 3 or more, so the first if risk_score >= 3 catches it first and the chain stops there.

Fix: put the highest threshold first, so each elif only gets the scores the branches above it turned down.

Code Runner Challenge

3.6 Popcorn 2 - Highest threshold first

View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 2 - Highest threshold first

risk_score = 9

# 3 thresholds -> 4 outcomes -> 4 answers
if risk_score >= 9:
    print("Do not reply. This is almost certainly a scam.")
elif risk_score >= 6:
    print("This looks risky. Check with someone you trust before replying.")
elif risk_score >= 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: The Trainer’s Scoring Step

The program first builds risk_score from three separate signals, then reports on it with an if / elif / else chain ordered from the highest threshold down.

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

message = "URGENT: send gift cards now to unlock your account"
amount_requested = 500
known_contact = False

risk_score = 0

# Step 1: score the message, one signal at a time.
# None of these needs an else: when a signal is missing, the score should simply
# stay the same, and "add nothing" is already what happens when the if is skipped.

# Signal 1: gift cards are a classic scam payment
if "gift card" in message.lower():
    risk_score += 4

# Signal 2: a large amount of money
if amount_requested > 100:
    risk_score += 3

# Signal 3: the sender is not someone they know
if not known_contact:
    risk_score += 2

print("Risk score:", risk_score)

# Step 2: report on the final score, highest threshold first.
# 3 thresholds -> 4 outcomes -> 4 answers
if risk_score >= 9:
    print("Do not reply. This is almost certainly a scam.")
elif risk_score >= 6:
    print("This looks risky. Check with someone you trust before replying.")
elif risk_score >= 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 ...

Proof That Every Branch Is Reachable

The score can be 0, 2, 3, 4, 5, 6, 7, or 9. These input values reach each of the four answers:

message amount_requested known_contact Score Answer
“URGENT: send gift cards now to unlock your account” 500 False 4 + 3 + 2 = 9 Do not reply. This is almost certainly a scam.
“Can you pay me back with a gift card?” 20 False 4 + 2 = 6 This looks risky. Check with someone you trust before replying.
“Can you send $250 for the car repair?” 250 True 3 Be a little careful, but this is probably fine.
“Hi Mom, are we still on for Sunday?” 0 True 0 Nothing suspicious in this message.