DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

Write a Python program to BMI calculator

BMI calculator:

BMI is a quick and inexpensive way to categorize a person's weight as underweight, normal, overweight, or obese.

BMI formula:

BMI is calculated by dividing weight by height squared:

Metric units:
BMI = weight (kg) / [height (m)]2

US customary units:
BMI = weight (pounds) / [height (in)]2 x 703

Example:

# Input the weight in kilograms
weight = float(input("Enter your weight (in kg): "))

# Input the height in meters
height = float(input("Enter your height (in meters): "))

# Calculate BMI using the formula: 
bmi = weight / (height ** 2)

# Output the calculated BMI value
print("BMI:", round(bmi, 2))  # Rounds BMI to 2 decimal places 

# Function to provide feedback based on BMI 
def bmi_feedback(bmi):
    if bmi < 18.5:
        return "You are underweight"
    elif 18.5 <= bmi <= 24.9:
        return "You have a healthy weight"
    elif 25 <= bmi <= 29.9:
        return "You are overweight"
    else:
        return "You are obese"

# Call the feedback function and store the result
bmi_result = bmi_feedback(bmi)

# Output the BMI category feedback
print("BMI Result:", bmi_result)
Enter fullscreen mode Exit fullscreen mode

Output:

Enter your weight:58
Enter your height:1.67
BMI: 20.796729893506402
BMI Result: You have a healthy weight
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)