How could I write a program to convert Km/h to m/s in python

First we need to know that the conversion factor for km/h to m/s is 3.6km/h=1m/s

Next we need to read in a value from the user. This can be done with 'input' function as follows:

KMHvalue= input("Enter a value in km/h: ")

we will then need to use our conversion to convert the value into m/s

MSvalue=KHMvalue/3.6

Then we can output the answer using the 'print' function:

print("your value in m/s is: "+MSvalue)

hence the whole program is:

KMHvalue= input("Enter a value in km/h: ")
MSvalue=KHMvalue/3.6
print("your value in m/s is: "+MSvalue)

We could make this program more sophisticated by allowing the user to select if they want to convert from km/h to m/s or the other way around. A sample script to do this would be:

choice=input("Please enter 1 to convert km/h to m/s or 2 to convert m/s to km/h")
​if choice==1
    KMHvalue= input("Enter a value in km/h: ")
    MSvalue=KHMvalue/3.6
    print("your value in m/s is: "+MSvalue)

else if choice==2
    MSvalue= input("Enter a value in m/s: ")
    KMHvalue=MSvalue*3.6
    print("your value in km/h is: "+KMHvalue)

else
​    print("Invalid Choice")

SW
Answered by Sarah W. Python tutor

13578 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Firstly, my question is not Python related, but maths A-level related. I don't know how to change that in my profile but I would like to only tutor maths GCSE and potentially A-level. My question: Integrate sin(x)^3 over x=0 and x=pi/2.


Why would I use dictionaries instead of a list?


Create an algorithm that can be used as a guessing game. Make sure to import random at the start.


What is the outcome of the following code? print(2*[3,4,5])


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences