Calculate BMI
Instructions
Write function bmi that calculates body mass index (bmi = weight / height2).
if bmi <= 18.5 return "Underweight"
if bmi <= 25.0 return "Normal"
if bmi <= 30.0 return "Overweight"
if bmi > 30 return "Obese"
Solution
def bmi(weight, height):
# Check for valid inputs
if weight <= 0 or height <= 0:
return None
# Calculate BMI using the formula
bmi = weight / height ** 2
# Return the corresponding category based on BMI ranges
if bmi <= 18.5:
return 'Underweight'
elif bmi <= 25:
return 'Normal'
elif bmi <= 30:
return 'Overweight'
else:
return 'Obese'
** 表示乘方
人生便是艺术。

浙公网安备 33010602011771号