3.6 Conditionals HW
3.6 Conditionals
POPCORN HACK 1
Code Runner Challenge
POPCORN HACK 1
View IPYNB Source
# CODE_RUNNER: POPCORN HACK 1
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 asking for a small amount of money.")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
POPCORN HACK 2
Code Runner Challenge
POPCORN HACK 2
View IPYNB Source
# CODE_RUNNER: POPCORN HACK 2
risk_score = 9
# Broken version with risk_score >= 3 first prints the mild warning for 9.
# Unreachable in the broken version: risk_score >= 6 and risk_score >= 9.
# Fixed order: 9, 6, 3, else.
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
Code Runner Challenge
HOMEWORK HACK
View IPYNB Source
# CODE_RUNNER: HOMEWORK HACK
message = "URGENT: send gift cards now to unlock your account"
amount_requested = 500
known_contact = False
risk_score = 0
# Add to risk_score using conditionals, one signal at a time.
# Each if is independent because a message can trigger multiple signals at once.
# No else is needed, because we want to keep adding points for each matching signal.
if "gift card" in message:
risk_score += 4
if amount_requested > 100:
risk_score += 3
if not known_contact:
risk_score += 2
# 4 thresholds -> 5 outcomes -> 5 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.")
elif risk_score >= 1:
print("A little suspicious. Keep an eye on it.")
else:
print("Nothing suspicious in this message.")
print(f"Final risk score: {risk_score}")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Reachability proof
Extreme Tests
Code Runner Challenge
TEST
View IPYNB Source
# CODE_RUNNER: TEST
message = "Hi Mom, are we still on for Sunday?"
amount_requested = 0
known_contact = True
risk_score = 0
# Each if is an independent signal, so false signals simply add 0.
if "gift card" in message:
risk_score += 4
if amount_requested > 100:
risk_score += 3
if not known_contact:
risk_score += 2
# 4 thresholds -> 5 outcomes -> 5 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.")
elif risk_score >= 1:
print("A little suspicious. Keep an eye on it.")
else:
print("Nothing suspicious in this message.")
print(f"Final risk score: {risk_score}")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
TEST
View IPYNB Source
# CODE_RUNNER: TEST
message = "URGENT: send gift cards now to unlock your account"
amount_requested = 500
known_contact = False
risk_score = 0
# Each if is an independent signal, so true signals can stack together.
if "gift card" in message:
risk_score += 4
if amount_requested > 100:
risk_score += 3
if not known_contact:
risk_score += 2
# 4 thresholds -> 5 outcomes -> 5 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.")
elif risk_score >= 1:
print("A little suspicious. Keep an eye on it.")
else:
print("Nothing suspicious in this message.")
print(f"Final risk score: {risk_score}")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...