DEV Community is a community of 604,851 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Build A Simple Weight Converter in Python β Beginners Tutorial π

Arvind Mehairjan Originally published at helloiamarra.com γ»2 min read

In this article, I am going to show you how you can build a simple weight converter application using Python.

The application works as follows:

The application asked the user to input the weight.

• After filling the weight the user receives whether they want to display in Kgs (kilograms) or in Lbs (pounds).
• The user will display a sentence what the weight is with the desired unit
• Additionally, there is also an option if the user entered invalid data.

Prerequisites

To build the weight converter app we need the following tools:

Python. You can download and install it on the official Python website. Make sure you download Python 3 and not Python 2 because Python 2 is not supported anymore.
An IDE. In this tutorial, I am going to use PyCharm. Download the Community Edition to freely use the software and install it.

Source code

Below you can find the full source code:

``````weight = float(input("What is your weight? "))
unit = input("Kgs or Lbs? ")
pound = 2.20462
converted_weight = float(weight * pound)
formatted_float = "{:.2f}".format(converted_weight)

print(weight)
print(unit)

if unit == "Kgs":
elif unit == "Lbs":
else:
print("That is not a valid input. Please try again.")
``````

Letβs break it down step by step.

`weight = float(input("What is your weight? "))`

In the code above we create the variable `weight` that will ask the user to put in their weight. It will have the type of float because the weight can have several decimals and doesnβt have to be a complete number.

`unit = input("Kgs or Lbs? ")`

After the user puts in their weight, the application asks whether they want to have their weight display in kilograms (kgs) or pounds (lbs).

`pound = 2.20462`

To calculate the value of the weight in pounds we need to create a separate variable called `pound` that holds the value for a pound. In this case, 1 kilogram is equal to 2.20462 pounds.

`converted_weight = float(weight * pound)`

Next, we create a variable that converts the weight the user put in into pounds. It has a float value and holds decimal. The problem, however, is that the converted weight has too many decimals. We only need two decimals for the weight in this case.

The solution for this is the variable `formatted_float` below:

`formatted_float = "{:.2f}".format(converted_weight)`

This variable format the variable `converted_weight` with two decimals using `:.2f.` This prints the value with two digits.

Afterward, we need to print the variables as well to display it to the user:

``````print(weight)
print(unit)
``````

Lastly, we created a simple if-else statement that prints the desired output based on the input of the user:

If the user chooses for kilograms, the program will print the message βYour weight is X Kgs.β Otherwise if the value of the unit is βLbsβ, the application will print βYour weight is X Lbs.β If both inputs of the value are invalid, there is the last value that will display that it is invalid and ask the user to try it again.

Thatβs it! This is how you can build a simple converter in Python 3. If youβre interested in more tips, make sure to join the newsletter!