3.6 Conditionals HW
Popcorn Hack 1
# 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.")
This message is not asking for a large amount of money.
Popcorn Hack 2
# 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.")
Do not reply. This is almost certainly a scam.
Homework Hack
# 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
# 1. Score the message using conditionals, one signal at a time
# These do not need an 'else' because we only want to add to the score if a risk is present; an absent risk simply leaves the score unchanged.
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
# 2. Report on the final score with an if / elif / else chain
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.")
Do not reply. This is almost certainly a scam.
Reachability Proof
Every branch in the reporting chain is reachable with the following inputs:
- Score >= 9 (Score = 9):
message = "gift card",amount_requested = 500,known_contact = False(4 + 3 + 2 = 9) - Score >= 6 (Score = 7):
message = "gift card",amount_requested = 500,known_contact = True(4 + 3 + 0 = 7) - Score >= 3 (Score = 4):
message = "gift card",amount_requested = 0,known_contact = True(4 + 0 + 0 = 4) - Score < 3 (Score = 0):
message = "clean",amount_requested = 0,known_contact = True(0 + 0 + 0 = 0)
Stretch Question: Using if with no else is correct for the scoring step because each risk factor is evaluated independently to accumulate a running total, whereas in Example A, the program’s entire output relied on a single check that remained completely silent when false.