Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to build a BMI Calculator in Python
Rishabh Singh ⚡
Rishabh Singh ⚡

Posted on

     

How to build a BMI Calculator in Python

Hola folks! Today we will build a simpleBMI Calculator in Python.

How does it work?

Alt Text

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 throughinput() function.

height=float(input("Enter your height in cm:"))weight=float(input("Enter your weight in kg:"))
Enter fullscreen modeExit fullscreen mode

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.

BMI=weight/(height/100)**2
Enter fullscreen modeExit fullscreen mode

Here we will be dividing theheight by 100 to convert thecentimetres intometers.

Now let's print out the BMI.

print(f"You BMI is{BMI}")
Enter fullscreen modeExit fullscreen mode

Now we have to print a statement to state the current health of the user based on theirBMI.

Here is how BMI is classified:

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 usingif conditionals for classification.

ifBMI<=18.4:print("You are underweight.")elifBMI<=24.9:print("You are healthy.")elifBMI<=29.9:print("You are over weight.")elifBMI<=34.9:print("You are severely over weight.")elifBMI<=39.9:print("You are obese.")else:print("You are severely obese.")
Enter fullscreen modeExit fullscreen mode

Here's what it will print:

  • if BMI isless than or equal to 18.4 thenYou are underweight. will be printed.
  • if BMI isless than or equal to 24.9 thenYou are healthy. will be printed.
  • if BMI isless than or equal to 29.9 thenYou are over weight. will be printed.
  • if BMI isless than or equal to 34.9 thenYou are severely over weight. will be printed.
  • if BMI isless than or equal to 39.9 thenYou are obese. will be printed.
  • if BMI none of the above are true thenYou 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

Support

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

If you like my work please considerBuying me a Coffee so that I can bring more projects, more articles for you.

https://dev-to-uploads.s3.amazonaws.com/i/5irx7eny4412etlwnc64.png

Also if you have any questions or doubts feel free to contact me onTwitter,LinkedIn &GitHub. Or you can also post a comment/discussion & I will try my best to help you :D

Top comments(6)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jaywalkerjpg profile image
JayWalker-jpg
  • Joined

Great project for a beginner like me. Thank you! I just have one problem, when I try this, it tells me:

BMI = (height * weight)
TypeError: can't multiply sequence by non-int of type 'str'

Any idea what might fix this?

CollapseExpand
 
adam_filip_9b9274b36e228f profile image
Adam Filip
  • Joined

when you input something python automaticly see it as a string. Make sure you put float in front of height and weight formula.
weight = float(input("Enter your weight in cm: "))
height = float(input("Enter your height in cm: "))

CollapseExpand
 
adam_filip_9b9274b36e228f profile image
Adam Filip
  • Joined

Also I think that BMI = (height * weight) is wrong. It should be
BMI = weight_kg / (height_cm/100)**2

CollapseExpand
 
life_of_thayow profile image
Life_of_thayow
I'm a python developer...

Tryspacng_hyhyjhjjb**uujujyyy

  1. **_
CollapseExpand
 
kelechi_odoemele_1174f540 profile image
Kelechi Odoemele
  • Joined

HOW TO ELIMINATE THIS ERROR Cell not executed due to pending input..The cell has not been executed to avoid kernel deadlock as there is another pending input! Submit your pending input and try again.

CollapseExpand
 
fudmel profile image
sonotoridesu
Student
  • Joined

Hey if you can see this can you add how to break while loop here because my task is to enter"0" to end the program or break the loop

Some comments may only be visible to logged-in visitors.Sign in to view all comments.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

💻 Full Stack Engineer | 🔆 Freelance UI/UX Designer | ⚡ Ambassador/Lead @microsoft @codecademy @codeforcauseIn | 👨‍🏫Teaching + ✍️Writing + 🛠Building
  • Location
    Mumbai, India
  • Education
    University of Mumbai
  • Joined

More fromRishabh Singh ⚡

DEV Community

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

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp