The request

From Debra Marshall, Warehouse Manager
Your goal
Set result to the number of boxes needed as an int: every box holds per_box candles and a partly filled box still counts as one box.
- 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 Debra gets, and why
The lesson
Three kinds of division
Python has three operators that deal with division, and analysts use all of them.
23 / 6 # 3.8333333333333335 true division, always a float
23 // 6 # 3 floor division, whole part only
23 % 6 # 5 modulo, the remainder
/ gives the exact answer as a float. // throws away the fractional part and rounds
down to the whole number below. % (read “modulo” or “mod”) gives what is left over after
the floor division. The two always fit together:
(23 // 6) * 6 + (23 % 6) # 18 + 5 = 23
Where analysts meet this
- Packing and batching: how many full pallets, how many boxes, how many pages of 50 results.
- Time conversions:
135 // 60is 2 hours and135 % 60is 15 minutes. - Every Nth row:
order_id % 10 == 0picks one order in ten for a quick sample. - Even and odd:
n % 2 == 0is true for even numbers.
Rounding up with math.ceil
Floor division always rounds down, which is wrong when a partial box still has to ship. You have two clean options.
The first uses the math module, which ships with Python. import makes it available, and
math.ceil (short for ceiling) rounds up to the next whole number:
import math
math.ceil(23 / 6) # 4
math.ceil(24 / 6) # 4 exactly 4 boxes, no extra
math.floor(23 / 6) # 3
The second uses // and % together: take the full boxes, then add one more box if anything is
left over. You’ll write that kind of logic yourself once you learn if in a later level.
math.ceil returns an int, which is exactly what you want for a count of boxes. Compare this
with the built-in round(), which rounds to the nearest number: round(23 / 6) is 4 here, but
round(25 / 6) is also 4 when you need 5 boxes. Rounding to nearest is the wrong tool for
“how many containers”.
Converting between int and float
Sometimes you need to change a number’s type on purpose:
int(3.83) # 3 cuts off the decimals (towards zero)
float(4) # 4.0
int("23") # 23 text that looks like a number becomes a number
int() truncates rather than rounds, so int(3.99) is 3. Don’t use it as a rounding function.
Common mistakes
- Using
//when the business rule says partial units count. 23 candles in boxes of 6 is 4 boxes, not 3. - Using
round()for container counts. It rounds to the nearest number, which can leave candles on the floor. - Forgetting
import math. Without it,math.ceilfails withNameError: name 'math' is not defined.
Interview note
“What does % do?” comes up surprisingly often in screening tests, usually as “find even order
IDs” or “convert minutes into hours and minutes”. Knowing that // and % are a matched pair
covers both.
Lantern lit!
Practised: floor divisionmodulomath ceilint
- Solved
- No hints
- First run
