Miles to kilometers Python

This function converts miles to kilometers (km). Complete the function by having it return the result, then call the function to convert 55 miles, and print "The distance in kilometers is " with the result of the function. Then calculate the round-trip in kilometers by doubling it, and print "The round-trip in kilometers is " with that number.

so far my code looks like this:

def convert_distance(miles):
km = miles * 1.6  # approximately 1.6 km in 1 mile
return km
miles = (55)    
km = convert_distance(miles)
print("The distance in kilometers is: " + str(km))
print("The round-trip in kilometers is: " + str(km * 2)) 

Anytime i try submitting it gives me this message pops-up: Not quite. Remember to use the str() function to convert numbers into strings when printing them with text, and to call the function with just one parameter, then accept the return value into a new variable.

This Python program using only a mathematics formula to convert kilometers to mile. We will take the distance in Kilometers while declaring the variables and then, calculate the distance in Miles using the formula. Finally, the result will be displayed on the screen.

# Python program to convert kilometers to miles

# take inputs
km = float(input('Enter distance in kilometers: '))

# conversion factor
conv_fac = 0.621371

# calculate Miles
mile = km * conv_fac

# display result
print('%0.2f kilometers is equal to %0.2f miles' %(km, mile))

Output for the different input values:-

Enter distance in kilometers: 5
5.00 kilometers is equal to 3.11 miles

Enter distance in kilometers: 8.3
8.30 kilometers is equal to 5.16 miles

Enter distance in kilometers: 308.264
308.26 kilometers is equal to 191.55 miles

Python Program to Convert Miles to Kilometers

In the previous program, we had converted kilometers to miles but in this program, we will convert miles to kilometers in Python. We will take the distance in Miles while declaring the variables and then, calculate the distance in Kilometers using the formula. Finally, the result will be displayed on the screen.

# Python program to convert miles to kilometers

# take inputs
mile = float(input('Enter distance in miles: '))

# conversion factor
conv_fac = 1.60934

# calculate kilometers
km = mile * conv_fac

# display result
print('%0.2f miles is equal to %0.2f kilometers' %(mile, km))

Output for the different input values:-

Enter distance in miles: 2.17
2.17 miles is equal to 3.49 kilometers

Enter distance in miles: 5
5.00 miles is equal to 8.05 kilometers

Enter distance in miles: 50.3
50.30 miles is equal to 80.95 kilometers

Also See:- Celsius to Fahrenheit in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

In this python example, we'll learn to convert miles taken from user to kilometers and display it.

Python Source Code: Miles to Kilometers


# miles input from the user
miles = float(input("Enter value in miles: "))

# calculate kilometers and display
kilometers = miles * 1.60934
print('%0.3f miles is equal to %0.3f kilometers' %(miles, kilometers))

Output

The output of the above program is:

Enter value in miles: 12
12.000 miles is equal to 19.312 kilometers

Bearing in mind that 1 mile is equal to approximately 1.61 kilometers, complete the program in the code editor so that it converts:

  • miles to kilometers;
  • kilometers to miles.

Challenge

kilometers = 12.25
miles = 7.38
miles_to_kilometers = ###
kilometers_to_miles = ###
print(miles, "miles is", round(miles_to_kilometers, 2), "kilometers")
print(kilometers, "kilometers is", round(kilometers_to_miles, 2), "miles")

Expected Output

7.38 miles is 11.88 kilometers
12.25 kilometers is 7.61 miles

After completing this article you will be able to write different converters, e.g., a USD to EUR converter, a temperature converter, etc. — let your imagination fly! Try to output the results by combining strings and variables. Try to use and experiment with the round() function to round your results to one, two, or three decimal places. Check out what happens if you don't provide any number of digits. Remember to test your programs.

Write a Python Program to Convert Miles to Kilometers. This Python example allows inserting the miles and converts them to kilometers. As we remember, one mile approximately equals 1.6093435 kilometers.

# Miles to Kilometers
 
miles = float(input("Enter the Miles = "))
 
kilometers = miles * 1.6093435
 
print("%.2f Miles equals %.2f Kilometers " %(miles, kilometers))
Miles to kilometers Python

Back to Categories Python Examples

Python Program to Convert Kilometers to Miles

Python Program to Create Tuple of Different Types

How do you convert miles to km in Python?

Understanding the Logic Behind the Conversion of Kilometre to Miles.
1 kilometre equals 0.62137 miles..
Miles = kilometre * 0.62137..
Kilometre = Miles / 0.62137..

How do you convert miles into km?

Ans: The distance in kilometers is calculated by multiplying the distance in miles by 1.609344. The conversion ratio utilized in the formula is 1.609344 kilometers because one mile equals 1.609344 kilometers.

How do you convert 1 km to miles?

We know that 1 km = 0.62137119 miles. So, 3 km = 3 × 0.62137119 = 1.86411357 miles.