The request

From Megan Collins, Head of Sales
Your goal
Set result to the shipping fee (a float) for the given subtotal and segment: 0.0 for segment 'business'; otherwise 7.95 if subtotal is below 50, 4.95 if it is at least 50 and below 75, and 0.0 if it is 75 or more.
- Store a number in
resultnot done yet
Your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
What Megan gets, and why
The lesson
True and False
A boolean (bool) has only two values: True and False. You get them from comparisons:
62.40 >= 50 # True
"consumer" == "business" # False
| operator | meaning |
|---|---|
== |
equal to |
!= |
not equal to |
< <= |
less than, less than or equal |
> >= |
greater than, greater than or equal |
String comparisons are exact and case-sensitive: "Business" == "business" is False. That’s
why you’ll often see .lower() before a comparison on data typed by people.
Combining conditions
and is true only when both sides are true. or is true when at least one side is. not flips a
value.
subtotal = 62.40
subtotal >= 50 and subtotal < 75 # True
segment == "business" or subtotal >= 75
not (subtotal < 50) # True
Python also lets you chain a range check, which reads like maths: 50 <= subtotal < 75.
if, elif, else
An if statement runs a block of code only when its condition is true. The block is the indented
lines under it, and the line before it ends with a colon:
if subtotal >= 75:
shipping = 0.0
elif subtotal >= 50:
shipping = 4.95
else:
shipping = 7.95
Python checks the conditions top to bottom and stops at the first true one. That’s why the
second condition can simply say subtotal >= 50: if the cart were 75 or more, the first branch
would already have run. elif (else if) can repeat as many times as you need, and else catches
everything left over. Both are optional.
Indentation is not decoration in Python; it defines which lines belong to the branch. Use four spaces consistently.
Order your rules from most specific
Business rules usually have exceptions (“business accounts always ship free”). Put the exceptions first, otherwise a general rule may grab the case before the exception is reached:
if segment == "business" or subtotal >= 75:
shipping = 0.0
elif subtotal >= 50:
shipping = 4.95
else:
shipping = 7.95
Watch the boundaries
“From $50 up to $75” is where bugs live. Is $75 itself in the middle band or free? The policy says
free from $75, so the check is >= 75, not > 75. When you read a rule, always ask what happens
exactly on the edge, and test a value right on it.
Common mistakes
- Writing
=instead of==in a condition.if segment = "business":is a syntax error. - Missing the colon at the end of
if,eliforelselines. - Getting the band order wrong, for example checking
subtotal >= 50beforesubtotal >= 75, so a $120 cart pays $4.95. - Writing
if segment == "business" or "enterprise":. The right side is a non-empty string, which always counts as true. Repeat the comparison:segment == "business" or segment == "enterprise".
Lantern lit!
Practised: booleanscomparisonif elif elseand or
- Solved
- No hints
- First run
