3.06 Conditionals HW
Popcorn Hack 1
Prediction: When the amount is 0, it will print “The requested amount is normal or zero.” When changed to an amount over 100, it will print “This message is asking for a large amount of money.”
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("The requested amount is normal or zero.")
Code Runner Challenge
3.6 Popcorn 1 - test the other outcome
View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 1 - test the other outcome
amount_requested = 150
if amount_requested > 100:
print("This message is asking for a large amount of money.")
else:
print("The requested amount is normal or zero.")
Actual output:
The requested amount is normal or zero.This message is asking for a large amount of money.
Popcorn Hack 2
Prediction: Since the risk_score is exactly 9, it will trigger the first condition (>= 9) and print the scam warning.
Code Runner Challenge
3.6 Popcorn 2 - fixed order, highest threshold first
View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 2 - fixed order, highest threshold first
risk_score = 9
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.")
Actual output:
Do not reply. This is almost certainly a scam.
Original (broken) order: with risk_score = 9, the buggy chain checked >= 3 first, so it printed “Be a little careful, but this is probably fine.” That’s dangerously wrong for the worst possible score.
Unreachable branches in the original: elif risk_score >= 6 and elif risk_score >= 9. Any score that is 6 or higher (or 9 or higher) is also >= 3, so the first if always catches it, and those two branches can never run for any score. The fix is to order thresholds highest first: >= 9, then >= 6, then >= 3, then else.
Homework
Prediction:
- “gift card” is in the message (+4)
- amount is 500 which is > 100 (+3)
- contact is not known (+2) Total risk score is 9. It will print the extreme risk warning.
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
# TODO 1: add to risk_score using conditionals, one signal at a time
# These do not need an else statement because if a signal isn't present,
# we simply want to do nothing and leave the score exactly as it is.
if "gift card" in message:
risk_score += 4
if amount_requested > 100:
risk_score += 3
if not known_contact:
risk_score += 2
# TODO 3: count comment in this form, with your real numbers:
# 3 thresholds -> 4 outcomes -> 4 answers
# TODO 2: report on the final score with an if / elif / else chain
if risk_score >= 9:
print("Do not engage. This is an extreme risk and likely a scam.")
elif risk_score >= 6:
print("High risk. Verify this person's identity before doing anything.")
elif risk_score >= 3:
print("Moderate risk. Be cautious and look out for other red flags.")
else:
print("Low risk. Proceed normally, but stay alert.")
Actual output:
Do not engage. This is an extreme risk and likely a scam.
Code Runner Challenge
3.6 Homework - reachability and extreme tests
View IPYNB Source
# CODE_RUNNER: 3.6 Homework - reachability and extreme tests
def score_and_advise(message, amount_requested, known_contact):
risk_score = 0
if "gift card" in message:
risk_score += 4
if amount_requested > 100:
risk_score += 3
if not known_contact:
risk_score += 2
# 3 thresholds -> 4 outcomes -> 4 answers
print("Score:", risk_score)
if risk_score >= 9:
print("Do not engage. This is an extreme risk and likely a scam.")
elif risk_score >= 6:
print("High risk. Verify this person's identity before doing anything.")
elif risk_score >= 3:
print("Moderate risk. Be cautious and look out for other red flags.")
else:
print("Low risk. Proceed normally, but stay alert.")
# Extreme test: every signal tripped
score_and_advise("URGENT: send gift cards now to unlock your account", 500, False)
# High-risk branch: gift card + stranger, small amount
score_and_advise("Can you buy me a gift card?", 50, False)
# Moderate branch: large amount from a known contact
score_and_advise("Can you lend me money for rent?", 300, True)
# Extreme test: completely clean message
score_and_advise("Hi Mom, are we still on for Sunday?", 0, True)
Reachability Proof
| Branch | Inputs (message / amount / known_contact) | Score | Output |
|---|---|---|---|
>= 9 |
“…send gift cards now…” / 500 / False | 4+3+2 = 9 | Do not engage… |
>= 6 |
“Can you buy me a gift card?” / 50 / False | 4+2 = 6 | High risk… |
>= 3 |
“Can you lend me money for rent?” / 300 / True | 3 | Moderate risk… |
else |
“Hi Mom, are we still on for Sunday?” / 0 / True | 0 | Low risk… |
Every branch has an input that reaches it, so there is no dead code. Extreme tests: the clean message (score 0) and the all-signals message (score 9) both print a sensible answer.