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)

Flowchart


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

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.

str_pace = input("At what pace do you run a km? e.g. 5:21")
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.

split_pace = str_pace.split(":")
minutes = int(split_pace[0])
seconds = int(split_pace[1])
pace = minutes * 60 + seconds
Step 3: Multiply this total by 42 as there are 42km in a Marathon
totalTime = pace * 42
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.

hours = totalTime // 3600
timeLeft = totalTime % 3600

minutes = timeLeft // 60
seconds = timeLeft % 60
Step 5: Display this new total on screen
print("You should complete a marathon in " + str(hours) + ":" + str(minutes) + ":" + str(seconds) +".")
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.

Testing


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
unlock-access

Solution...

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

Did you like this challenge?

Click on a star to rate it!

Average rating 2.8 / 5. Vote count: 8

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: , , ,