024 實例5-身體質量指數BMI

1、"身體質量指數BMI"問題分析

1.1 身體質量指數BMI

BMI:對身體質量的刻畫框架

  • BMI:Body Mass Index:國際上經常使用的衡量人體肥胖和健康程度的重要標準,主要用於統計分析
  • 定義:\(BMI = 體重 (kg) / 身高^2 (m^2)\)spa

  • 實例:體重 72 kg,身高 1.75 m
    • BMI 值是 23.5
  • 這個值是否健康呢?code

國際:世界衛生組織 國內:國家衛生健康委員會orm

分類 國際BMI值\((kg/m^2)\) 國內BMI值\((kg/m^2)\)
偏瘦 <18.5 <18.5
正常 18.5 ~ 25 18.5 ~ 24
偏胖 25 ~ 30 24 ~ 28
肥胖 ≥30 ≥28

1.2 問題需求

-輸入:給定體重和身高值
-輸出:BMI指標分類信息(國際和國內)input

2、"身體質量指數BMI"實例講解

2.1 身體質量指標BMI

思路方法it

  • 難點在於同時輸出國際和國內對應的分類
  • 思路1:分別計算並給出國際和國內BMI分類
  • 思路2:混合計算並給出國際和國內BMI分類
分類 國際BMI值\((kg/m^2)\) 國內BMI值\((kg/m^2)\)
偏瘦 <18.5 <18.5
正常 18.5 ~ 25 18.5 ~ 24
偏胖 25 ~ 30 24 ~ 28
肥胖 ≥30 ≥28

2.1.1 國際

# CalBMIv1.py

height, weight = eval(input("請輸入身高(米)和體重\(公斤)[逗號隔開]: "))
bmi = weight / pow(height, 2)
print("BMI 數值爲:{:.2f}".format(bmi))

who = ""
if bmi < 18.5:
    who = "偏瘦"
elif 18.5 <= bmi < 25:
    who = "正常"
elif 25 <= bmi < 30:
    who = "偏胖"
print("BMI 指標爲:國際'{0}'".format(who))
請輸入身高(米)和體重\(公斤)[逗號隔開]: 1.8,70
BMI 數值爲:21.60
BMI 指標爲:國際'正常'

2.1.2 國內

# CalBMIv2.py

height, weight = eval(input("請輸入身高(米)和體重\(公斤)[逗號隔開]: "))
bmi = weight / pow(height, 2)
print("BMI 數值爲:{:.2f}".format(bmi))

nat = ""
if bmi < 18.5:
    nat = "偏瘦"
elif 18.5 <= bmi < 24:
    nat = "正常"
elif 25 <= bmi < 38:
    nat = "偏胖"
print("BMI 指標爲:國內'{0}'".format(nat))
請輸入身高(米)和體重\(公斤)[逗號隔開]: 1.8,70
BMI 數值爲:21.60
BMI 指標爲:國內'正常'
# CalBMIv3.py

height, weight = eval(input("請輸入身高(米)和體重\(公斤)[逗號隔開]: "))
bmi = weight / pow(height, 2)
print("BMI 數值爲:{:.2f}".format(bmi))
who, nat = "", ""
if bmi < 18.5:
    who, nat = "偏瘦", "偏瘦"
elif 18.5 <= bmi < 24:
    who, nat = "正常", "正常"
elif 24 <= bmi < 25:
    who, nat = "正常", "偏胖"
elif 25 <= bmi < 28:
    who, nat = "偏胖", "偏胖"
elif 28 <= bmi < 30:
    who, nat = "偏胖", "肥胖"
else:
    who, nat = "肥胖", "肥胖"
print("BMI 指標爲:國際'{0}', 國內'{1}'".format(who, nat))
請輸入身高(米)和體重\(公斤)[逗號隔開]: 1.8,70
BMI 數值爲:21.60
BMI 指標爲:國際'正常', 國內'正常'

3、"身體質量指數BMI"觸類旁通

關注多分支條件的組合table

  • 多分支條件之間的覆蓋是重要問題
  • 程序可運行,但不正確,要注意多分支
  • 分支結構是程序的重要框架,讀程序先看分支
相關文章
相關標籤/搜索