Calculate Body Mass Index Using Python

This code defines two functions: calculate_bmi and calculate_ideal_weight. The calculate_bmi function takes in two parameters, weight and height, and calculates the Body Mass Index (BMI) of a person based on those values. The calculate_ideal_weight function takes in a single parameter, height, and calculates the ideal weight for a person based on their height.

The code then prompts the user to enter their weight and height, and asks them to select an option (1 or 2). If the user selects option 1, the code calls the calculate_bmi function to calculate the BMI based on the user’s weight and height, and then prints the result to the user. It then determines the weight category based on the BMI value and prints a message to the user indicating whether they are underweight, at a normal weight, overweight, or obese. If the user selects option 2, the code calls the calculate_ideal_weight function to calculate the ideal weight based on the user’s height and prints the result to the user. If the user selects an invalid option, the code prints an error message.

def calculate_bmi(weight, height):
  """Calculate the BMI by dividing the weight in kilograms by the height in meters squared."""
  bmi = weight / (height ** 2)
  return bmi

def calculate_ideal_weight(height):
  """Calculate the ideal weight by multiplying the height in meters by itself and then multiplying the result by 25."""
  return height * height * 25

def determine_weight_category(bmi):
  """Determine the weight category based on the BMI."""
  if bmi < 18.5:
    return 'underweight'
  elif bmi >= 18.5 and bmi < 25:
    return 'normal weight'
  elif bmi >= 25 and bmi < 30:
    return 'overweight'
  else:
    return 'obese'

# Prompt the user to enter their weight in kilograms
while True:
  try:
    weight = float(input('Weight (kg): '))
    if weight <= 0:
      print("Error: Weight must be a positive value.")
    else:
      break
  except ValueError:
    print("Error: Weight must be a numeric value.")

# Prompt the user to enter their height in meters
while True:
  try:
    height = float(input('Height (m): '))
    if height <= 0:
      print("Error: Height must be a positive value.")
    else:
      break
  except ValueError:
    print("Error: Height must be a numeric value.")

print("Select an option:")
print("1. Calculate BMI")
print("2. Calculate ideal weight")

# Prompt the user to select an option (1 or 2)
while True:
  try:
    choice = int(input())
    if choice != 1 and choice != 2:
      print("Error: Invalid choice.")
    else:
      break
  except ValueError:
    print("Error: Choice must be a numeric value.")

if choice == 1:
  # Calculate the BMI
  bmi = calculate_bmi(weight, height)
  print(f'Your BMI is {bmi:.2f}')

  # Determine the weight category
  weight_category = determine_weight_category(bmi)
  print(f'You are {weight_category}')
elif choice == 2:
  # Calculate the ideal weight
  ideal_weight = calculate_ideal_weight(height)
  print(f'Your ideal weight is {ideal_weight:.2f} kg')

 

Leave the first comment