How to Convert Kilometers to Miles and Miles to Kilometers.

 How to convert Kilometers to Miles and Miles to Kilometers

In this program, we will learn how to convert kilometers to miles or miles to kilometers by taking input from the user.

Before we take the input from the user let's see how to perform the conversion without an input from user.

Kilometers  to  Miles

# declearing a variable and assigning a value to it
kilo = 38

# conversion factor
conversion_factor = 0.621371

# Multiplying the conversion_factor by the value to get kilometers
miles = kilo * conversion_factor

# converting int/float to a string and printing the results
print(str(kilo) + ' kilometers in miles is ' + str(miles))

In the above code, we have converted kilometers to miles by multiplying kilo with conversion_factor because 1 kilometer = 0.621371 miles.




Miles  to  Kilometers

Let's see how we can also convert miles to kilometers without taking user input

# declearing a variable and assigning a value to it
miles = 26.58

# conversion factor
conversion_factor = 0.621371

# Multiplying the conversion_factor by the value to get kilometers
kilometers = miles / conversion_factor

# converting int/float to a string and printing the results
print(str(miles) + ' miles in kilometers is ' + str(kilometers))

When converting miles to kilometers, you have to divide the miles by the conversion_factor because 1 mile = 0.621371 kilometers.




Conversion by Taking User Input

Since we don't know different users will be converting different values of kilometers to miles or the vice versa, we need to take an input from the user in order to perform the conversion for any value the user enters. That's, let's make our converter responsive.

Kilometers   to   Miles with User Input

# creating variable and assigning the user input to it
kilometers = float(input('ENTER THE KILOMETERS: '))

# conversion factor
# 1 kilometer = 0.621371 miles
con_factor = 0.621371

# multiplying kilometers by the conversion factor to get miles
miles = kilometers * con_factor

# printing the answer to the user
print(miles)

# converting int/float to str inorder to concatenate them with strings
print(str(kilometers) + ' Kilometers in Miles is ' + str(miles))




Miles   To   Kilometers with User Input

# creating variable and assigning the user input to it
miles = float(input('ENTER THE MILES: '))

# conversion factor
# 0.621371 miles = 1 kilometer
con_factor = 0.621371

# dividing kilometers by the conversion factor to get miles
kilos = miles / con_factor

# converting int/float to str inorder to concatenate them with strings
print(str(miles) + 'Miles in Kilometers is ' + str(kilos))






Multi Converter

Let's make our converter flexible, that is, let us make the converter to convert either kilometers to miles or miles to kilometers by asking the user to decide.


print("----------------- Enter KTM to convert Kilometers to Miles or MTK to convert Miles to Kilometers----------------------------")
option = input('DO YOU WANT TO CONVERT KILOMETERS TO MILES [KTM]/ MILES TO KILOMETER [MTK]? ').lower()

if option == 'ktm':
kilometers = float(input('ENTER THE KILOMETERS: '))

con_factor = 0.621371
miles = kilometers * con_factor
print(str(kilometers) + 'Kilometers in Miles is ' + str(miles))

elif option == 'mtk':
miles = float(input('ENTER THE MILES: '))

con_factor = 0.621371
kilos = miles / con_factor
print(str(miles) + ' Miles in Kilometers is ' + str(kilos))

else:
print('INVALID INPUT! ENTER KTM TO CONVERT KILOMETERS TO MILES OR MTK TO CONVERT MILES TO KILOMETERS')


Kilometers to Miles

As you can see, when the user input ktm, the program allows the user to enter kilometers to convert it to miles.

Let's see what if the user happens to input mtk instead

Miles to Kilometers


Subscribe My YouTube Channel and Follow Me

YouTube

TikTok 

Twitter

Facebook 

Post a Comment

1 Comments