Python BMI Calculator – A Complete Step-by-Step Tutorial

In this article, we will learn how to develop a Body Mass Index (BMI) Calculator using the Python programming language. But before we begin developing one, let us first define Body Mass Index (BMI).

Body Mass Index (BMI):

BMI, or Body Mass Index, is a measure of relative weight based on an individual’s weight and height. The Body Mass Index is commonly used to classify people based on their height and weight. These classifications include underweight, healthy, overweight, and even obesity. Furthermore, it is being used by a number of countries to promote healthy eating.

Body Mass Index (BMI) can be used as a substitute for direct measures of body fat. Furthermore, BMI is a low-cost and simple means of screening for weight classes that may cause health problems.

Let Us Understand the BMI Calculator’s Operation

A BMI Calculator takes an individual’s weight and height and computes their Body Mass Index (BMI).

The data below illustrates how BMI is classified in order to determine a person’s health state.

  • If your BMI is less than 18.5, it falls within the underweight range.
  • If your BMI is 18.5 to 24.9, it falls within the normal or Healthy Weight range.
  • If your BMI is 25.0 to 29.9, it falls within the overweight range.
  • If your BMI is 30.0 or higher, it falls within the obese range.

The Formula for Calculating BMI:

BMI  = (weight)/(height)^2

where weight  in – kg

and height in – m

Python Code

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the height as static input and store it in a variable.
  • Give the weight as static input and store it in another variable.
  • Calculate the Body Mass Index(BMI) value using the above given mathematical formula and store it in another variable.
  • Check if the above obtained BMI value is greater than 0 using the if conditional statement.
  • If it is true, then check again if the above obtained BMI value is less than or equal to 16.
  • If it is true, then print “Person is severely underweight. Please Takecare”.
  • Check if the above obtained BMI value is less than or equal to 18.5 using the elif conditional statement.
  • If it is true, then print “Person is Underweight”.
  • Check if the obtained BMI value is less than or equal to 25 using the elif conditional statement.
  • If it is true, then print “Person is Healthy”.
  • Check if the obtained BMI value is less than or equal to 30 using the elif conditional statement.
  • If it is true, then print “Person is Overweight”.
  • Else print “Person is suffering from Obesity”.
  • Else print “Invalid Input”.
  • The Exit of the Program.

Below is the implementation:

# Give the height as static input and store it in a variable.
gvn_height = 2.5
# Give the weight as static input and store it in another variable.
gvn_weight = 45
# Calculate the Body Mass Index(BMI) value using the above given mathematical
# formula and store it in another variable.
rslt_BMI = gvn_weight/(gvn_height*gvn_height)
print("The BMI value for the given height{",
      gvn_height, "}", "and weight{", gvn_weight, "}=", rslt_BMI)
# Check if the above obtained BMI value is greater than 0 using the if conditional
# statement.
if(rslt_BMI > 0):
    # If it is true, then check again if the above obtained BMI value is less than
    # or equal to 16. 
    if(rslt_BMI <= 16):
        # If it is true, then print "Person is severely underweight.Please Takecare".
        print("Person is severely underweight. Please Takecare")
    # Check if the above obtained BMI value is less than or equal to 18.5 using
    # the elif conditional statement. 
    elif(rslt_BMI <= 18.5):
        # If it is true, then print "Person is Underweight".
        print("Person is Underweight")
    # Check if the obtained BMI value is less than or equal to 25 using the elif
        # conditional statement.
    elif(rslt_BMI <= 25):
        # If it is true, then print "Person is Healthy".
        print("Person is Healthy")
    # Check if the obtained BMI value is less than or equal to 30 using the elif
        # conditional statement. 
    elif(rslt_BMI <= 30):
        # If it is true, then print "Person is Overweight".
        print("Person is Overweight")
    else:
        # Else print "Person is suffering from Obesity".
        print("Person is suffering from Obesity")
# Else print "Invalid Input".
else:
    print("Invalid Input")

Output:

The BMI value for the given height{ 2.5 } and weight{ 45 }= 7.2
Person is severely underweight. Please Takecare

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the height as user input using the float(input()) function and store it in a variable.
  • Give the weight as user input using the float(input()) function and store it in another variable.
  • Calculate the Body Mass Index(BMI) value using the above given mathematical formula and store it in another variable.
  • Check if the above obtained BMI value is greater than 0 using the if conditional statement.
  • If it is true, then check again if the above obtained BMI value is less than or equal to 16.
  • If it is true, then print “Person is severely underweight. Please Takecare”.
  • Check if the above obtained BMI value is less than or equal to 18.5 using the elif conditional statement.
  • If it is true, then print “Person is Underweight”.
  • Check if the obtained BMI value is less than or equal to 25 using the elif conditional statement.
  • If it is true, then print “Person is Healthy”.
  • Check if the obtained BMI value is less than or equal to 30 using the elif conditional statement.
  • If it is true, then print “Person is Overweight”.
  • Else print “Person is suffering from Obesity”.
  • Else print “Invalid Input”.
  • The Exit of the Program.

Below is the implementation:

# Give the height as user input using the float(input()) function and store it in a variable.
gvn_height = float(input("Enter height(m) = "))
# Give the weight as user input using the float(input()) function and store it in another variable.
gvn_weight = float(input("Enter Weight(Kg) = "))
# Calculate the Body Mass Index(BMI) value using the above given mathematical
# formula and store it in another variable.
rslt_BMI = gvn_weight/(gvn_height*gvn_height)
print("The BMI value for the given height{",
      gvn_height, "}", "and weight{", gvn_weight, "}=", rslt_BMI)
# Check if the above obtained BMI value is greater than 0 using the if conditional
# statement.
if(rslt_BMI > 0):
    # If it is true, then check again if the above obtained BMI value is less than
    # or equal to 16. 
    if(rslt_BMI <= 16):
        # If it is true, then print "Person is severely underweight.Please Takecare".
        print("Person is severely underweight. Please Takecare")
    # Check if the above obtained BMI value is less than or equal to 18.5 using
    # the elif conditional statement. 
    elif(rslt_BMI <= 18.5):
        # If it is true, then print "Person is Underweight".
        print("Person is Underweight")
    # Check if the obtained BMI value is less than or equal to 25 using the elif
        # conditional statement.
    elif(rslt_BMI <= 25):
        # If it is true, then print "Person is Healthy".
        print("Person is Healthy")
    # Check if the obtained BMI value is less than or equal to 30 using the elif
        # conditional statement. 
    elif(rslt_BMI <= 30):
        # If it is true, then print "Person is Overweight".
        print("Person is Overweight")
    else:
        # Else print "Person is suffering from Obesity".
        print("Person is suffering from Obesity")
# Else print "Invalid Input".
else:
    print("Invalid Input")

Output:

Enter height(m) = 1.5
Enter Weight(Kg) = 59
The BMI value for the given height{ 1.5 } and weight{ 59.0 }= 26.22222222222222
Person is Overweight