3.6 Conditionals HW
3.6 Conditionals — Homework (Scam-Message Trainer)
Lesson: https://pages.opencodingsociety.com/python/conditionals/py
Theme: the scam-message trainer from the Friends of Poway Seniors capstone. Every condition is a question that returns True or False, and every question needs all of its answers written down.
College Board connection: selection uses a Boolean condition to choose which statements run. In Python, = stores a value and == asks a question. A chain of three thresholds has four outcomes, so it needs four answers.
Popcorn Hack 1 — the silent failure
The original trainer checked only the case the author pictured: if amount_requested > 100 with no else. When the amount was 0, the program printed nothing, raised nothing, and exited normally. That is a bug, not an error — silence looks the same as “this message is fine.”
Fix: one condition has two outcomes, so both outcomes now print.
Output recorded: This message is not asking for a large amount of money.
Code Runner Challenge
Popcorn Hack 1 — Fix the silent conditional so both outcomes print, and add the count comment above the if.
Popcorn Hack 2 — the chain in the wrong order
The original chain started with if risk_score >= 3, so a score of 9 printed “Be a little careful, but this is probably fine.” — the wrong advice for the worst possible score.
Unreachable branches in the original code: elif risk_score >= 6 and elif risk_score >= 9. The first if risk_score >= 3 catches every score of 3 or higher, so neither later branch can ever run.
Fix: order the thresholds highest first, so each branch only sees scores that already failed the branches above it.
Output recorded: Do not reply. This is almost certainly a scam.
Code Runner Challenge
Popcorn Hack 2 — Run the broken chain first, then fix the order so no branch is unreachable.
Homework Hack — build the trainer’s scoring step
The starter hands us risk_score already calculated. This version computes the score from three independent signals, then reports the result with a four-answer if / elif / else chain.
Scoring rules: +4 if the message mentions gift cards, +3 if the amount requested is over 100, +2 if the sender is not a known contact.
Why each scoring if has no else: each signal is independent. If a signal is absent, the score should simply stay where it is — there is no alternate answer to print. Adding an else would imply the signal had a second outcome that needed its own message, which it does not. This is different from Example A, where the missing else hid the “safe message” answer from the user.
Reporting chain: 3 thresholds -> 4 outcomes -> 4 answers. The thresholds are ordered highest first, so every branch is reachable.
Code Runner Challenge
Homework Hack — Score a message from three independent signals, then report the score with a four-answer if / elif / else chain.
Reachability proof — one input set per branch
| Branch | Risk score | Message mentions gift cards? | Amount > 100? | Known contact? | Why it reaches this branch |
|---|---|---|---|---|---|
if risk_score >= 9 |
9 | yes (+4) | yes (+3) | no (+2) | all three signals trip |
elif risk_score >= 6 |
7 | yes (+4) | yes (+3) | yes (+0) | both text and amount signals trip |
elif risk_score >= 3 |
4 | yes (+4) | no (+0) | yes (+0) | only the gift-card signal trips |
else |
0 | no (+0) | no (+0) | yes (+0) | no signals trip |
Every branch has at least one reachable input, and every possible score from 0 upward lands in exactly one branch. The final else guarantees that no input falls off the end without output.
Extreme test output
All signals trip: Risk score: 9 -> Advice: Do not reply. This is almost certainly a scam.
Clean message: Risk score: 0 -> Advice: Nothing suspicious in this message.
Stretch answer: using if with no else is fine for the scoring step because each signal is an independent add-on; an absent signal correctly contributes 0 and needs no message. In Example A, the if controlled the program’s only user-facing decision, so the missing else silently swallowed the safe-message answer.
Notes for submission: 3.6 Conditionals popcorn + homework complete |
Notebook: <published URL> |