top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Writer's pictureNtandoyenkosi Matshisela

Speed Converter

For people who are not arithmetically savvy, it is hard to convert speed from km/hr to miles/hr and vice versa. Furthermore, to convert meters/second to km/hr and vice versa.


The following python code is an attempt to assist this dilemma:


The first part is to define a function that takes either values of km/hr or miles/hr. The arithmetic function will convert using the formula. Later the user is then asked to input a value and the code outputs the desired figure.

#Kilometers Function
def kmh_to_mph(x):
    return round(x * 0.621371, 2) 

# Miles Function
def mph_to_kmh(x):    
    return round(1/ 0.621371 * x, 2)

speed_km = float(input("What is the speed in km/hr?:"))
g = kmh_to_mph(speed_km)
print("The speed in miles/hr is {}".format(g))


speed_m = float(input("What is the speed in miles/hr?:"))
g = mph_to_kmh(speed_m)
print("The speed in km/hr is {}".format(g))

To address the second part of meters/sec to km/hr and vice versa. Again an arithmetic equation using 3.6 is used. To validate the outputs, the inverse function of the arithmetic operations are used. That is, if 3.6*x was used, to validate, 1/3.6 *x was used to return to the previous value.




def kmh_to_ms(x):    
	return round(1/ 3.6 * x, 2)

def ms_to_kmh(x):    
	return round(3.6 * x, 2) 

speed_ms = float(input("What is the speed in metres/s?:"))
g = ms_to_kmh(speed_ms)
print("The speed in km/hr is {}".format(g))
speed_kmh = float(input("What is the speed in km/hr?:"))
g = kmh_to_ms(speed_kmh)
print("The speed in metres/sec is {}".format(g))

The Github repository can be found here









3 comments

Recent Posts

See All

3 Comments


Data Insight
Data Insight
Sep 15, 2021

You link is not clickable. You should test it. Check how others link to their git repos and ask questions.

Like

Data Insight
Data Insight
Sep 15, 2021

What does the statement 'To validate the inverse figures are executed' mean? Again do not insert codes using images, use the code snippet functionality instead or host it on GitHub gist. That way your code is more useful to a reader if they wanted to use part of your code. Moreover you didn't provide any link to the git repo.

Like
Replying to

Noted, I hope I have addressed the concerns

Like
bottom of page