Python Program Logic: Python Code Kilometers To Miles Conversion is a program to input a value in kilometers. This Python program then converts the given value of kilometers provided by the user at run time, into miles.
How to convert kilometers to miles?
Since we know that 1 kilometer is equal to 0.621371 miles:
Hence we will multiply the given kilometers by 0.621371
Kilometers To Miles Conversion Formula
miles = kilometers * 0.621371
Python Program Source Code
# Use input() function to take kilometers from the user # use float() function to convert string input into real number kilometers = float(input("Enter value in kilometers: ")) # Since we know that 1 kilometer is equal to 0.621371 miles # conversion factor conversion_factor = 0.621371 # calculate miles from kilometers miles = kilometers * conversion_factor # Use round(value,decimal_places) function to round # the kilometers up to two decimal places, similarly with miles print(round(kilometers,2),' Kilometers = ', round(miles,2), ' Miles')
Sample Run Output of Kilometers to miles program
Enter value in kilometers: 125
125.0 Kilometers = 77.67 Miles
>>>
Enter value in kilometers: 10
10.0 Kilometers = 6.21 Miles
>>>
Enter value in kilometers: 55
55.0 Kilometers = 34.18 Miles
>>>