Converting Kilometers to Miles and Miles to Kilometers

A code to convert Kilometers to Miles and vice versa.

Welcome to the code! This code helps the beginners to use a while loop to convert Kilometers to Miles and vice versa in a user-friendly way.

In this code, the most important line of code that we need to know is the conversion factor (1 Km = 0.621371 miles). Further, we use a while loop to make the conversion depending on the user’s choice.

Kilometers to Miles: miles = kilometers * 0.621371
Miles to Kilometers: kilometers = miles / 0.621371

Note: Loved the post? You too can publish your article on “Python for fun” which will be loved by millions. Contribute an article!

					repeat = "yes"

while repeat == "yes" or repeat == "y":
    print("1. Convert Kilometers to Miles")
    print("2. Convert Miles to Kilometers")
    choice = int(input("Choose the conversion---"))

    if choice == 1:
        kilometers = float(input("Enter the kilometers value---"))
        miles = kilometers * 0.621371 #conversion factor = 0.621371(1 km = 0.621371 miles)
        print(kilometers," kilometers is equal to ",miles," miles")
    elif choice == 2:
        miles = float(input("Enter the miles value---"))
        kilometers = miles / 0.621371
        print(miles," miles is equal to ",kilometers," kilometers")
        print("Wrong Input. Choose again!")
        repeat = "yes"
    repeat = input("Do you want to convert again?")
0 0 votes
Notify of
Inline Feedbacks
View all comments