Loading

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'

** 表示乘方

posted @ 2023-03-11 17:01  Artwalker  阅读(51)  评论(0)    收藏  举报
Live2D