How to build a BMI Calculator in Python
Tags: python, tutorial, codenewbie, beginners
Published: 2021-02-17 06:11:23

Hola folks! Today we will build a simple **BMI Calculator in Python**.

## How does it work?

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxxpcij3thqzj09mrbxc.png)

A BMI Calculator will take in the height and weight of the individual and will calculate the BMI of the person. 

**Body mass index (BMI) is a measure of body fat based on height and weight.** 

Based on the BMI of the individual, it will print a statement stating the overall health of the person.

Let's start coding our project!

## Let's Code

Alright, so the first thing we need to do is to ask the user their height & weight. This can be easily achieved through **`input()`** function.

```python
height = float(input("Enter your height in cm: "))
weight = float(input("Enter your weight in kg: "))
```

We will convert the input string to float so that we can perform calculations with it.

Next up, we have to calculate the BMI. 

**The formula to calculate BMI is $weight (kg)/{height (m)}^2$.**  Let's implement this formula in python.

```python
BMI = weight / (height/100)**2
```

Here we will be dividing the **`height`** by 100 to convert the **centimetres** into **meters**.

Now let's print out the BMI.

```python
print(f"You BMI is {BMI}")
```

Now we have to print a statement to state the current health of the user based on their **`BMI`**.

**Here is how BMI is classified:**

![https://miro.medium.com/max/960/1*S2aR2zRzjiV2IVK6DUMNhw.jpeg](https://miro.medium.com/max/960/1*S2aR2zRzjiV2IVK6DUMNhw.jpeg)

We are going to simplify it a bit, so make it easier to understand but feel free to stick to this classification if you prefer.

We will be using **`if`** conditionals for classification.

```python
if BMI <= 18.4:
    print("You are underweight.")
elif BMI <= 24.9:
    print("You are healthy.")
elif BMI <= 29.9:
    print("You are over weight.")
elif BMI <= 34.9:
    print("You are severely over weight.")
elif BMI <= 39.9:
    print("You are obese.")
else:
    print("You are severely obese.")
```

**Here's what it will print:**

- if BMI is **`less than or equal to 18.4`** then **`You are underweight.`** will be printed.
- if BMI is **`less than or equal to 24.9`** then **`You are healthy.`** will be printed.
- if BMI is **`less than or equal to 29.9`** then **`You are over weight.`** will be printed.
- if BMI is **`less than or equal to 34.9`** then **`You are severely over weight.`** will be printed.
- if BMI is **`less than or equal to 39.9`** then **`You are obese.`** will be printed.
- if BMI none of the above are true then **`You are severely obese.`** will be printed.

That's it! We are done! Easy Peasy right!

## Source Code

You can find the complete source code of this project here -

[mindninjaX/Python-Projects-for-Beginners](https://github.com/mindninjaX/Python-Projects-for-Beginners/tree/master/BMI%20Calculator)

## Support

Thank you so much for reading! I hope you found this beginner project useful. 

If you like my work please consider [Buying me a Coffee](https://buymeacoff.ee/mindninjaX) so that I can bring more projects, more articles for you.

![https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png](https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png)

Also if you have any questions or doubts feel free to contact me on [Twitter](https://twitter.com/mindninjaX), [LinkedIn](https://www.linkedin.com/in/mindninjax/) & [GitHub](https://github.com/mindninjaX). Or you can also post a comment/discussion & I will try my best to help you :D