Whether you are calculating the speed of a car, estimating how far a sound wave has travelled, or working out how long it takes light to reach the Earth, the relationship between speed, distance and time is used in many areas of science, engineering and everyday life.
The relationship is described using the following formula:
Speed = Distance ÷ Timeor, using letters and the / arithmetic symbol used by computer scientists instead of the ÷ symbol:
s = d / t
The same formula can be rearranged depending on which value we need to calculate:
- Speed: s = d / t
- Distance: d = s × t
- Time: t = d / s
Python Challenge
In this Python challenge, you will create one program capable of performing all three calculations.
Your program should start by asking the user what they would like to calculate:
What would you like to calculate? s - Speed d - Distance t - Time
The program will then need to make a decision based on the user’s answer.
For example, if the user chooses distance, the program should ask them to enter a speed and a time. It can then calculate the missing distance using:
distance = speed × timeIf they choose speed, the program will need the distance and time, while calculating time will require the distance and speed.
This means your program will need to use selection with an if / elif / else statement to decide which calculation to perform.

Python Code
Complete the Python code below:
Example 1 – Calculating the Speed of a Car
Imagine a car travels 180 km in 2 hours.
The user selects s because they want to calculate the speed. Your program asks for the distance and time before calculating:
180 ÷ 2 = 90 km/hSo the program could display:
The speed is 90 km/h.
Example 2 – How Fast Does Sound Travel?
We can also use our program for a simple science experiment.
Imagine you are standing some distance from a large wall. You make a loud noise and measure how long it takes before you hear the echo.
Suppose the sound travels a total distance of 686 metres and takes 2 seconds to make the journey.
Using:
Speed = Distance ÷ Timewe get:
686 ÷ 2 = 343 m/sThis gives an approximate value for the speed of sound in air, which is around 343 metres per second under typical room-temperature conditions.
Remember: when using an echo, the sound travels to the wall and back again, so its total distance travelled is twice your distance from the wall.
Example 3 – The Speed of Light
Your program can even work with much larger numbers.
Light travels incredibly quickly through a vacuum: approximately 300,000 km/s.
The average distance between the Sun and Earth is about 150,000,000 km. We can therefore calculate approximately how long sunlight takes to reach us:
Time = Distance ÷ Speed 150,000,000 ÷ 300,000 = 500 secondsThat’s about 8 minutes and 20 seconds.
So when you look at the Sun, you are actually seeing light that left it more than eight minutes ago!
Programming Skills
To complete this challenge, you will need to combine several important Python programming skills:
- Using input() to collect information from the user
- Storing data using variables
- Converting user inputs to float or int values
- Performing arithmetic calculations
- Using if, elif and else statements
- Displaying calculated results using print(), and string concatenation
Extension Challenges
Once your basic program is working, see if you can improve it.
- Challenge 1: Ask the user which units they are using and include these units when displaying the answer.
Challenge 2: Round calculated answers to two decimal places.
Challenge 3: Detect invalid choices. What should happen if the user enters x instead of s, d or t?
Challenge 4: Add a loop so that the user can perform several calculations without restarting the program.

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






