How to Create a Mile to Kilometer Converter in Python?

Estimated read time 1 min read

To create a mile to kilometer converter in Python, you can write a simple script that reads in a distance in miles and converts it to kilometers using the conversion factor 1 mile = 1.60934 kilometers. Here’s an example:

# read in distance in miles
miles = float(input("Enter distance in miles: "))

# convert miles to kilometers
kilometers = miles * 1.60934

# print the result
print("{0} miles is equal to {1:.2f} kilometers".format(miles, kilometers))

In this script, we first use the input function to read in a distance in miles as a floating-point number. We then use the conversion factor 1.60934 to convert the miles to kilometers, and store the result in the variable kilometers. We use the print function to display the result to the user, using the format method to insert the values of miles and kilometers into the string.

To test the script, you can run it in a Python interpreter or save it as a Python file (e.g., and run it from the command line using the command python

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply