Marathon Time Calculator

marathonIn this challenge you are going to write a Python script to help a marathoner predict the overall time they can complete a Marathon in (42km).

This estimation will be based on the runner’s pace which is the time they take to run 1km.

For instance:

  • Runner A runs 1km in 5:25 (5 minutes and 25 seconds)
  • Runner B, who is slower, runs 1km in 6:10 (6 minutes and 10 seconds)

Your Python script will:

  1. INPUT: Ask the runner for their pace (e.g. 5:25),
  2. PROCESS: Convert this input into a number of seconds: e.g. 5:25 = 5 * 60 + 25 = 325 seconds,
  3. PROCESS: Multiply this total by 42 as there are 42km in a Marathon,
  4. PROCESS: Convert this new total using the hh:mm:ss format. (e.g 3:47:30)
  5. OUTPUT: Display this new total on screen (using the hh:mm:ss format). (e.g 3:47:30)


The following flowchart describes the main steps of our algorithm based on the
Input > Process > Output model:

Your Solution

Code your solution to this challenge:

Step by Step Solution:

The 5 steps described above are solved to help you complete this challenge step by step:

Step 1: Input the pace in the mm:ss format
This step is fairly straightforward. We will use an input() function to retrieve the user input and a variable called str_pace to store this input as a string.
Step 2: Convert this input into a number of seconds
This step is more complex as we need to extract the left and right side of the input. In python we can split a string using a character as a separator, in this case the colon (:). We will also use the int() function to convert (cast) a string into an integer.
Step 3: Multiply this total by 42 as there are 42km in a Marathon
Step 4: Convert this new total using the hh:mm:ss format
The totalTime tells us how many seconds will the runner complete the Marathon in. Considering that there are 3600 seconds in one hour and 60 seconds in one minute we can use the DIV (//) (whole division) and MOD (%) (remainder operators to convert our total time in the required format.
Step 5: Display this new total on screen
Note: To be 100% accurate you should time the pace by 42.195 instead of 42 as the exact length of a Marathon is 42km and 195 meters.


Now that you have implemented your solution you may want to test it by completing the following test plan: (this test plan is based on a length of 42km)

Test # Input Values Expected Output Actual Output
#1 Pace: 5:25 3:47:30
#2 Pace: 6:10 4:19:00
#3 Pace: 4:00 2:48:00
#4 Pace: 2:55 (Close to World Record) 2:02:30


The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members Area
Tagged with: , , ,