Here's a Python program that calculates the Body Mass Index (BMI) for a user and categorizes it based on the BMI value:
def calculate_bmi(weight, height):
return weight / (height ** 2)
def classify_bmi(bmi):
if bmi < 18.5:
return "Underweight"
elif 18.5 <= bmi < 25:
return "Normal weight"
elif 25 <= bmi < 30:
return "Overweight"
else:
return "Obese"
def main():
print("Welcome to the BMI Calculator!")
# Get user input for weight and height
try:
weight = float(input("Please enter your weight (in kg): "))
height = float(input("Please enter your height (in meters): "))
if weight <= 0 or height <= 0:
print("Weight and height must be positive numbers.")
return
# Calculate BMI
bmi = calculate_bmi(weight, height)
# Print the result
print(f"Your BMI is: {bmi:.2f}")
classification = classify_bmi(bmi)
print(f"You are classified as: {classification}")
except ValueError:
print("Invalid input. Please enter numeric values for weight and height.")
if __name__ == "__main__":
main()
How to Use the Program
- Copy the code into a Python environment (such as an IDE or a Jupyter notebook).
- Run the program.
- Input your weight in kilograms and height in meters when prompted.
- The program will calculate your BMI and classify your weight status.
Additional Notes
- The program includes basic error handling to ensure that the user inputs valid numeric values for weight and height.
- It checks to make sure that both weight and height are positive numbers before performing calculations.