3.06 Conditionals

Source lesson: python/conditionals

Learning Objective: Build a condition out of a comparison, give every condition all of its answers, and order an elif chain so no branch is unreachable.

Submission notes

  • 3.6 Conditionals popcorn + homework complete.
  • Published page: https://jupiterian.opencodingsociety.com/python/conditionals-hw
  • Popcorn completion: yes (Popcorn 1 else added, Popcorn 2 reordered, unreachable branches named)
  • Homework metrics: 3 signals (+4 / +3 / +2), max score 9, 3 thresholds → 4 outcomes → 4 answers, tests: clean → 0, all signals → 9, boundaries 6 and 3
  • Language: Python

Popcorn

Popcorn Hack 1: The silent failure

Step 1: Run as given with amount_requested = 0, it prints nothing. There’s no error and it exits normally, so it’s a bug, not an error. 0 > 100 is False and nothing handles False, so that outcome just falls off the end. To the reader, silence looks like “this message is fine.”

Step 2: Added the missing else and the count comment.

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 ...

Actual output: This message is not asking for a large amount of money. Both outcomes print now. 500 gives the first message and 0 gives the second.

Popcorn Hack 2: The chain in the wrong order

Step 1: Run as given with risk_score = 9, the trainer says “Be a little careful, but this is probably fine.” That’s the worst possible score getting the mildest advice.

Step 2, unreachable branches: elif risk_score >= 6 (“This looks risky…”) and elif risk_score >= 9 (“Do not reply…”) can never run. Any score that’s 6+ or 9+ is also 3+, so the first if always grabs it first. An elif only gets checked if everything above it was False, and a score can’t be under 3 and also be 6 or more.

Step 3: Reordered so the highest threshold goes first.

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 ...

Actual output: Do not reply. This is almost certainly a scam. Every branch is now reachable: 9 and 10 → if, 6 to 8 → first elif, 3 to 5 → second elif, 0 to 2 → else.

Extra: every score from 0 to 10

Not part of the lesson page. The same fixed chain runs inside a loop over every score from 0 to 10, so all four branches can be seen firing in one run.

Code Runner Challenge

3.6 Popcorn 2 Extra - run the fixed chain on every score from 0 to 10

View IPYNB Source
# CODE_RUNNER: 3.6 Popcorn 2 Extra - run the fixed chain on every score from 0 to 10
# 3 thresholds -> 4 outcomes -> 4 answers
for x in range(0, 11):
    risk_score = x
    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

Homework Hack: Build the trainer’s scoring step

Starting from the supplied starter, risk_score is now computed instead of handed in:

Signal Condition Points
Mentions gift cards "gift card" in message.lower() +4
Asks for over $100 amount_requested > 100 +3
Sender is not a known contact not known_contact +2

Prediction (starter inputs): the message mentions gift cards (+4), $500 is over 100 (+3), and the sender is unknown (+2), so the score is 9 → “Do not reply.”

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: if a signal isn't there, it adds 0, and the chain below always prints
if "gift card" in message.lower():
    risk_score += 4
if amount_requested > 100:
    risk_score += 3
if not known_contact:
    risk_score += 2

print("Risk score:", risk_score)

# 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 ...

Actual output: Risk score: 9, then Do not reply. This is almost certainly a scam. This matches the prediction.

Why each scoring if needs no else: a signal that is missing contributes 0 points, and “do nothing” is exactly the right answer for that outcome. So each if has 2 outcomes (add points or add nothing) and both are handled correctly. The scoring step also never talks to the user. The reporting chain does, and its final else gives every score an answer.

Stretch: why if with no else is fine here but was a bug in Example A: in Example A the missing branch was the only thing that would have told the user anything, so its silence reached them as “this message is fine.” Here the missing branch just means “add 0,” and the reporting chain after it always prints.

Reachability proof

The only possible scores are sums of 4, 3 and 2: 0, 2, 3, 4, 5, 6, 7, 9. The top score is 9, so every score from 0 up to the maximum lands in exactly one branch. This table gives one set of inputs for each answer:

Branch Scores it catches Inputs that reach it Score
if risk_score >= 9 9 message = "URGENT: send gift cards now to unlock your account", amount_requested = 500, known_contact = False 4 + 3 + 2 = 9
elif risk_score >= 6 6, 7 message = "Grandma, buy GIFT CARDS for me", amount_requested = 0, known_contact = False 4 + 2 = 6
elif risk_score >= 3 3, 4, 5 message = "Can you send $250 for the plumber?", amount_requested = 250, known_contact = True 3 = 3
else 0, 2 message = "Hi Mom, are we still on for Sunday?", amount_requested = 0, known_contact = True 0

Each row is run as a test below. No branch is dead code: the elif chain goes from the highest threshold to the lowest, so an elif only runs when the score is below every threshold above it.

Tests

Each test is a fresh run where only the three inputs change. Language: Python.

Test 1: Every signal tripped (extreme high)

Prediction: 9 → if → “Do not reply.”

Code Runner Challenge

3.6 Homework - Test 1: every signal tripped

View IPYNB Source
# CODE_RUNNER: 3.6 Homework - Test 1: every signal tripped
message = "FINAL NOTICE: buy $1000 in gift cards or your bank account closes"
amount_requested = 1000
known_contact = False

risk_score = 0

if "gift card" in message.lower():
    risk_score += 4
if amount_requested > 100:
    risk_score += 3
if not known_contact:
    risk_score += 2

print("Risk score:", risk_score)

# 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 ...

Test 2: Completely clean message (extreme low)

Prediction: 0 → else → “Nothing suspicious.”

Code Runner Challenge

3.6 Homework - Test 2: clean message

View IPYNB Source
# CODE_RUNNER: 3.6 Homework - Test 2: clean message
message = "Hi Mom, are we still on for Sunday?"
amount_requested = 0
known_contact = True

risk_score = 0

if "gift card" in message.lower():
    risk_score += 4
if amount_requested > 100:
    risk_score += 3
if not known_contact:
    risk_score += 2

print("Risk score:", risk_score)

# 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 ...

Test 3: Boundary at 6 (uppercase gift card mention, no money, unknown sender)

Prediction: 4 + 2 = 6 → first elif → “Check with someone you trust.” This also checks that .lower() catches “GIFT CARDS.”

Code Runner Challenge

3.6 Homework - Test 3: boundary at 6

View IPYNB Source
# CODE_RUNNER: 3.6 Homework - Test 3: boundary at 6
message = "Grandma, buy GIFT CARDS for me"
amount_requested = 0
known_contact = False

risk_score = 0

if "gift card" in message.lower():
    risk_score += 4
if amount_requested > 100:
    risk_score += 3
if not known_contact:
    risk_score += 2

print("Risk score:", risk_score)

# 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 ...

Test 4: Boundary at 3 (money only, known contact)

Prediction: 3 → second elif → “Be a little careful.”

Code Runner Challenge

3.6 Homework - Test 4: boundary at 3

View IPYNB Source
# CODE_RUNNER: 3.6 Homework - Test 4: boundary at 3
message = "Can you send $250 for the plumber?"
amount_requested = 250
known_contact = True

risk_score = 0

if "gift card" in message.lower():
    risk_score += 4
if amount_requested > 100:
    risk_score += 3
if not known_contact:
    risk_score += 2

print("Risk score:", risk_score)

# 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 ...
Test Inputs (gift card / amount / known) Expected Actual Branch Pass
All signals yes / 1000 / False 9, Do not reply 9, Do not reply if Yes
Clean no / 0 / True 0, Nothing suspicious 0, Nothing suspicious else Yes
Boundary 6 yes (uppercase) / 0 / False 6, Check with someone 6, Check with someone first elif Yes
Boundary 3 no / 250 / True 3, Be a little careful 3, Be a little careful second elif Yes

All four branches ran, and each test printed a score and an answer, so no input falls off the end. Tests 3 and 4 land exactly on a threshold, which shows that >= includes the boundary value.

Design Thinking

Step Scam trainer notes
Need An older adult receives a message and needs clear advice on whether it is a scam. Silence or a confidently wrong answer is worse than no trainer.
Goal Turn three yes/no signals into one score, then give exactly one piece of advice for every possible score, with no silent inputs and no unreachable advice.
Methods considered One combined condition with and (only 2 answers, so it can’t grade severity), nested conditionals (4+ answers, but that is 3.07’s topic), and additive scoring followed by an ordered if / elif / else chain (chosen).
Prototype The starter plus three scoring ifs, using "gift card" in message, and the chain from Example B.
Test The starter message scored 9 correctly, but a message written in capitals, “buy GIFT CARDS,” missed the +4 because in is case-sensitive. That dropped a 6 to a 2 (“Nothing suspicious”), the same failure the lesson warns about.
Revision I changed the check to "gift card" in message.lower() and reran all four tests. Test 3 now scores 6 and gets “Check with someone you trust.” I also added a Risk score: line so the reader can see why each piece of advice was given.