Python Program To Calculate Body Mass Index

In this Python programming tutorial, you will learn how you can write a Python program to calculate the body mass index.

Body mass index (BMI) is a measure of body fat based on a person’s weight and height. It is commonly used to screen for weight categories that may lead to health problems. BMI is calculated by dividing a person’s weight (in kilograms) by their height (in meters) squared.

The formula for calculating BMI is:

BMI = weight (kg) / (height (m) x height (m))

The resulting number is then used to determine whether a person is underweight, normal weight, overweight, or obese. The following categories are commonly used:

  • Underweight: BMI below 18.5
  • Normal weight: BMI between 18.5 and 24.9
  • Overweight: BMI between 25 and 29.9
  • Obese: BMI of 30 or higher

Simple Code

heightInFeet = float(input('Enter height in feet : '))
heightInMeters = 0.3048 * heightInFeet;
weight = float(input('Enter weight in kilograms : '))
BMI = weight / heightInMeters**2
print('Your BMI is',BMI)

Output

Enter height in feet : 6
Enter weight in kilograms : 80
Your BMI is 23.9198009260216

Exception Handling

while True:
    try:
        heightInFeet = float(input('Enter height in feet : '))
        heightInMeters = 0.3048 * heightInFeet;
        weight = float(input('Enter weight in kilograms : '))
        BMI = weight / heightInMeters**2
        print('Your BMI is ',BMI)
        break
    except:
        print('Invalid Input. Enter again')

Enter height in feet : text
Invalid Input. Enter again
Enter height in feet : 6
Enter weight in kilograms : 80
Your BMI is 23.9198009260216

Explained: Python Program To Calculate Body Mass Index

#this program will run on loop until it is executed successfully!
while True:
    #first python will try the code written in the try block
    #if any occurs, the code of the except block will be executed and program will run again
    try:
        #taking input from user for height and weight
        heightInFeet = float(input('Enter height in feet : '))
        heightInMeters = 0.3048 * heightInFeet;
        weight = float(input('Enter weight in kilograms : '))
        #below is the formula to calculate BMI
        BMI = weight / heightInMeters**2
        print('Your BMI is ',BMI)
        #if everything run successfully till now, this break program will end the program
        break
    #if the try block fails to execute, i.e. user enters invalid input such as a text, this except block will be executed and the program will run again
    except:
        print('Invalid entry. Enter again')

Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading