BMI Calculator in Python
BMI Calculator in Python
Introduction
This is a simple Python program that calculates the Body Mass Index (BMI) of a user based
on their height and weight.
Source Code
def calculate_bmi(weight, height):
bmi = weight / (height ** 2)
return bmi
def main():
print("BMI Calculator")
weight = float(input("Enter your weight in kg: "))
height = float(input("Enter your height in meters: "))
bmi = calculate_bmi(weight, height)
print(f"Your BMI is: {bmi:.2f}")
if bmi < 18.5:
print("You are underweight.")
elif 18.5 <= bmi < 24.9:
print("You have a normal weight.")
elif 25 <= bmi < 29.9:
print("You are overweight.")
else:
print("You are obese.")
if __name__ == "__main__":
main()
Explanation
This Python script asks the user to input their weight (in kilograms) and height (in meters),
calculates the BMI using the standard formula (weight divided by square of height), and
categorizes the result into health ranges.
Sample Output
Enter your weight in kg: 70
Enter your height in meters: 1.75
Your BMI is: 22.86
You have a normal weight.
Conclusion
The BMI Calculator is a great beginner exercise to get comfortable with input/output,
functions, and conditional logic in Python.
How to Run
Save the script as `bmi_calculator.py`, and run it using `python bmi_calculator.py`.