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)

1 answer

Certainly! Below is a simple Python program that calculates the Body Mass Index (BMI) based on user input for weight and height, and then categorizes the result.

def calculate_bmi(weight, height):
    return weight / (height ** 2)

def get_bmi_category(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
    weight = float(input("Please enter your weight in kilograms: "))
    height = float(input("Please enter your height in meters: "))
    
    # Calculate BMI
    bmi = calculate_bmi(weight, height)
    
    # Determine the BMI category
    category = get_bmi_category(bmi)
    
    # Print the results
    print(f"Your BMI is: {bmi:.2f}")
    print(f"You are classified as: {category}")

if __name__ == "__main__":
    main()

How to Use the Program

  1. Copy the code into a Python environment (like a text editor, or an IDE such as PyCharm or Jupyter Notebook).
  2. Run the program.
  3. Follow the prompts to enter your weight in kilograms and height in meters.
  4. The program will calculate your BMI and provide you with a classification based on the BMI value.

Notes

  • Ensure that you input valid numerical values for weight and height.
  • The BMI formula is defined as \( \text{BMI} = \frac{\text{weight}}{\text{height}^2} \).