English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this example, we will learn to convert kilometers to miles and display them.
To understand this example, you should understand the followingPython programmingTopic:
# Get the user's input in kilometers kilometers = float(input("Enter the value in kilometers:")) # Conversion coefficient conv_fac = 0.621371 # Calculate mileage miles = kilometers * conv_fac print('%0.2f kilometers equal to %0.2f miles '%(kilometers,miles))
Output result
Enter the value in kilometers: 10 10.00 kilometers is equal to 6.21 miles
Here, the user is prompted to enter kilometers. This value is stored in the kilometers variable.
Because1kilometers is equal to 0.621371miles, so we can get the equivalent miles by multiplying the kilometers by this factor.
It's your turn:Modify the above program to use the following formula to convert miles to kilometers and then run it.
kilometers = miles / conv_fac