Can you build an Irish Rail journey calculator without rewriting a single conversion?
Your Irish Rail journey log gives you distance in metres and time in minutes, but you want the average speed in kilometres per hour. If you've already tested both conversions, how can your new program use them as they are?
Pass thirty thousand metres into your tested distance function. Its parameter holds that value locally while the function runs. The function returns thirty kilometres, and your caller can store that result. The conversion itself doesn't need to print anything.
Save your two tested functions in conversions dot py. The first docstring states that the function takes metres and returns kilometres. The second gives minutes as the input and hours as the result. This Python file is a module that your journey calculator can import.
Your journey log records a duration of ninety minutes. Look at the function name and its docstring before you decide what to pass in. Which call gives you that duration in hours? Pick an answer.
Read the plan from top to bottom. Assume the user enters numbers. Read the distance once, then keep asking for a time until it’s greater than zero. Call the two tested functions in order, store their results, divide kilometres by hours and display the speed to one decimal place.
Save journey dot py beside the module. Import both names, leaving dot py off the import. Read the distance, then ask for a time. If it’s positive, break out of the loop. Otherwise, print the warning and ask again. Call both functions, divide kilometres by hours and print to one decimal place.
Before running the full program, work out what it should print for thirty thousand metres and thirty minutes. You expect thirty kilometres and half an hour. Thirty divided by half is sixty, so look for sixty point zero kilometres per hour.
Now try a journey that lasts an hour and a half. If you enter forty-five thousand metres and ninety minutes, what should the calculator display on its final speed line? Pick the output you expect to see.
Both functions can pass their own tests and the calculator can still get the wrong answer. If you divide by the original ninety minutes, you get zero point five kilometres per minute. Use the returned hours instead, and the number matches the kilometres per hour label.
Run journey dot py with each row’s data. For thirty thousand metres and thirty minutes, check for sixty point zero kilometres per hour. Start again with twelve thousand metres. Enter zero, check that it warns you and asks again, then enter twenty. Record each actual result and mark pass only if it matches.