The request

From Daniel Brooks, Head of Operations
Your goal
Fix the code so result is the text label for the total route time in whole hours and leftover minutes, in exactly the format "2 h 15 min" (a str). The three legs are already in the starter.
- Store a text value (str) 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 Daniel gets, and why
The lesson
Reading a bug from its output
The label says “2.25 h 15 min”. Two clues are in there. First, 2.25 has decimals, so hours is a
float: something used true division. Second, 0.25 hours is exactly 15 minutes, so the leftover is
being counted twice, once inside the hours and once in the minutes. Reading the wrong output
carefully often tells you which line to look at before you even open the code.
The legs add up to 48 + 52 + 35 = 135 minutes:
135 / 60 # 2.25 true division: hours with a fraction
135 // 60 # 2 floor division: whole hours only
135 % 60 # 15 modulo: the minutes left over
The matched pair
// and % are designed to be used together. Floor division answers “how many whole 60s fit in”,
and modulo answers “what is left over”. They always fit back together:
(135 // 60) * 60 + 135 % 60 # 120 + 15 = 135
That makes them the standard tool for splitting any quantity into big and small units: minutes into hours and minutes, days into weeks and days, units into full cases and loose units.
Once both parts are whole numbers, the f-string does the rest. f"{hours} h {rest} min" puts each
value into the text, and because both are ints there are no stray decimals.
Check the fix on more than one value
A fix that only works for today’s route isn’t a fix. Try a few totals in your head or in the editor: 60 minutes should be “1 h 0 min”, 59 should be “0 h 59 min”, and 150 should be “2 h 30 min”. The edge cases, exactly on an hour and just below one, are where conversion bugs hide.
Common mistakes
- Replacing
/withround(minutes / 60). It rounds to the nearest hour, so it happens to work for 135 minutes but turns 165 minutes into “3 h 45 min”. Python also rounds halves to the even number (round(2.5)is 2), which makes the bug even harder to spot in testing. - Formatting the float with
:.0f. The label then shows “2 h”, but 2.75 hours would print as “3 h”. - Changing the modulo line.
minutes % 60was already correct.
Lantern lit!
Practised: debuggingfloor divisionmodulof strings
- Solved
- No hints
- First run
