Create a program that calculates the Body Mass Index (BMI) for a user.

The program should ask the user for their weight (in kilograms) and height (in meters).
It should calculate BMI using the formula: BMI= wt / ht**2​
wtThe program should then determine and print whether the user is:
"Underweight" (BMI < 18.5)
"Normal weight" (BMI 18.5–24.9)
"Overweight" (BMI 25–29.9)
"Obese" (BMI ≥ 30)
Create the program as using python

1 answer

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

  1. Copy the code into a Python environment (such as an IDE or a Jupyter notebook).
  2. Run the program.
  3. Input your weight in kilograms and height in meters when prompted.
  4. 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.