Home > Backend Development > Python Tutorial > Write a Python program to BMI calculator

Write a Python program to BMI calculator

Patricia Arquette
Release: 2024-11-19 02:07:02
Original
528 people have browsed it

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)
Copy after login

Output:

Enter your weight:58
Enter your height:1.67
BMI: 20.796729893506402
BMI Result: You have a healthy weight
Copy after login

The above is the detailed content of Write a Python program to BMI calculator. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template