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 Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay