An amount can fall into three buckets: under $20 is "low", $20 to under $100 is "medium", $100 or more is "high". Two thresholds, three outcomes. How many branches do you need in code?
Three? One for each outcome?
Exactly — and Python has if/elif/else for this. Conditions are checked top-to-bottom and the first True branch wins:
if amount < 20:
return "low"Start with the smallest bucket. Chain the rest with elif and close with else.
What if my order was wrong — like, what if I put amount < 100 first?
Then 10.0 would match amount < 100 on the first check and never reach the "low" branch. Order from most specific (smallest range) to most general (largest). Get the order wrong and the buckets silently collapse.
if amount < 20:
return "low"
elif amount < 100:
return "medium"
else:
return "high"At the boundary — exactly 20 — which bucket does it land in?
20 < 20 is False, so it skips the first branch. 20 < 100 is True, so it lands in "medium". Strict < means 20 is the first medium value. If you wanted 20 to be low, you'd use <= on the first condition. Pick your operator based on where you want the edge.
Order matters and operator choice matters. Once both are right, the function routes cleanly.
Three branches, two thresholds, one function. And the same shape scales to four or five buckets if you ever need more.
if / elif / elseTL;DR: check conditions top-to-bottom; first True wins.
else — catches every case the earlier branches didn't| Value | < 20 | < 100 | result |
|---|---|---|---|
19.99 | True | — | "low" |
20.00 | False | True | "medium" |
99.99 | False | True | "medium" |
100.00 | False | False | "high" |
< vs <= is where the edge lands — choose explicitly.
Write `classify_expense(amount)` that returns `"low"` for amounts under 20, `"medium"` for 20 up to under 100, and `"high"` for 100 or more.
Tap each step for scaffolded hints.
No blank-editor panic.
An amount can fall into three buckets: under $20 is "low", $20 to under $100 is "medium", $100 or more is "high". Two thresholds, three outcomes. How many branches do you need in code?
Three? One for each outcome?
Exactly — and Python has if/elif/else for this. Conditions are checked top-to-bottom and the first True branch wins:
if amount < 20:
return "low"Start with the smallest bucket. Chain the rest with elif and close with else.
What if my order was wrong — like, what if I put amount < 100 first?
Then 10.0 would match amount < 100 on the first check and never reach the "low" branch. Order from most specific (smallest range) to most general (largest). Get the order wrong and the buckets silently collapse.
if amount < 20:
return "low"
elif amount < 100:
return "medium"
else:
return "high"At the boundary — exactly 20 — which bucket does it land in?
20 < 20 is False, so it skips the first branch. 20 < 100 is True, so it lands in "medium". Strict < means 20 is the first medium value. If you wanted 20 to be low, you'd use <= on the first condition. Pick your operator based on where you want the edge.
Order matters and operator choice matters. Once both are right, the function routes cleanly.
Three branches, two thresholds, one function. And the same shape scales to four or five buckets if you ever need more.
if / elif / elseTL;DR: check conditions top-to-bottom; first True wins.
else — catches every case the earlier branches didn't| Value | < 20 | < 100 | result |
|---|---|---|---|
19.99 | True | — | "low" |
20.00 | False | True | "medium" |
99.99 | False | True | "medium" |
100.00 | False | False | "high" |
< vs <= is where the edge lands — choose explicitly.