How to Create a Kilometer to Miles Converter in Python

In this tutorial, we will create a "How to Create a Kilometer to Miles Converter in Python". This tutorial will teach you how to program a converter from kilometers to miles. We will tackle and try to understand the concept of this program and learn to code this converter app. I will provide a sample program to demonstrate the actual coding of this tutorial.

This tutorial is simple and easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program will try to convert the kilometer value into miles. I will do my best to provide you with the most easiest method of creating this program, called "Kilometer to Miles Converter". So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console of kilometer to miles converter. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. while True:
  2.  
  3. ret=False
  4.  
  5. print("\n\n================ Kilometer to Miles Converter App ================\n\n")
  6. kilometers = float(input("Enter value in kilometers: "))
  7.  
  8.  
  9. conv_fac = 0.621371
  10.  
  11.  
  12. miles = kilometers * conv_fac
  13. print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))
  14.  
  15. user_input=input("\n\nDo you want to convert again? (Yes/No): ")
  16. print("\n\n")
  17. if user_input.lower()=='no':
  18. print("Exit program.")
  19. break
  20.  
  21. elif user_input.lower()=='yes':
  22. ret=True
  23.  
  24. else:
  25. print("Wrong input.")
  26. ret=True

In the code that I provided, the first thing you will see is that we enclose the whole code with a while loop to ensure the continuous running of the program.

Next, we set some variables that will take the kilometer value and also set the conversion factor. We will now use a formula to calculate the conversion for the miles value.

Output:

The How to Create a Kilometer to Miles Converter in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Kilometer to Miles Converter in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment