CSP 3.6 Conditionals

Completed both Popcorn Hacks and the scam-message trainer homework.

Popcorn Hack 1: The Silent Failure

Code Runner Challenge

3.6 Popcorn 1 - Run it first. Then make both outcomes print.

View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 1 - Run it first. Then make both outcomes 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

Code Runner Challenge

3.6 Popcorn 2 - Run it first. Then fix the order.

View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 2 - Run it first. Then fix the order.

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: Score a Message, Then Advise on It

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

# No else needed on these: a missing signal just adds 0 points.
if "gift card" in message.lower():
    risk_score = risk_score + 4
if amount_requested > 100:
    risk_score = risk_score + 3
if not known_contact:
    risk_score = risk_score + 2

print("Risk score:", risk_score)

# 3 thresholds -> 4 outcomes -> 4 answers
if risk_score >= 7:
    print("Do not reply. This message has several scam warning signs.")
elif risk_score >= 4:
    print("This looks risky. Check with someone you trust before replying.")
elif risk_score >= 1:
    print("Be a little careful with this message.")
else:
    print("Nothing suspicious in this message.")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Reachability Proof

Answer Inputs Score
Do not reply gift card, 500, unknown contact 9
Looks risky gift card, 0, known contact 4
Be a little careful no gift card, 0, unknown contact 2
Nothing suspicious no gift card, 0, known contact 0

Code Runner Challenge

3.6 Homework tests - clean message and all signals

View IPYNB Source
# CODE_RUNNER: 3.6 Homework tests - clean message and all signals

for message, amount_requested, known_contact in [
    ("See you at dinner Sunday!", 0, True),
    ("URGENT: send gift cards now", 500, False),
]:
    risk_score = 0
    if "gift card" in message.lower():
        risk_score = risk_score + 4
    if amount_requested > 100:
        risk_score = risk_score + 3
    if not known_contact:
        risk_score = risk_score + 2

    print(message, "-> score", risk_score)
    if risk_score >= 7:
        print("Do not reply. This message has several scam warning signs.")
    elif risk_score >= 4:
        print("This looks risky. Check with someone you trust before replying.")
    elif risk_score >= 1:
        print("Be a little careful with this message.")
    else:
        print("Nothing suspicious in this message.")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...