1. Lighthouse

Level 6 · Debug: the route time looks wrong

The request

From Daniel Brooks, Head of Operations

“The driver sheet says today's route takes "2.25 h 15 min". That can't be right. Can you fix the script that writes the label?”

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

Python

Loading the editor…

    ResultPress Run to see yours

    Mote checks your result

    Press Run. Each row that matches the goal becomes a firefly in your lantern.

    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 / with round(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 % 60 was already correct.

    Pick what may run on this site. You can change this any time from "Cookie settings" at the bottom of every page.