Files

41 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def calc_bmi(height_cm, weight_kg):
"""输入身高(cm)、体重(kg),返回 BMI 值(保留一位小数)"""
height_m = height_cm / 100
bmi = weight_kg / (height_m ** 2)
return round(bmi, 1)
def judge(bmi):
"""按中国成人标准判定(与 WHO 国际标准略有不同)"""
if bmi < 18.5:
return "偏瘦"
elif bmi < 24:
return "正常"
elif bmi < 28:
return "超重"
else:
return "肥胖"
if __name__ == "__main__":
while True:
try:
height = float(input("请输入身高(cm),例如 170:"))
weight = float(input("请输入体重(kg),例如 60:"))
if height <= 0 or weight <= 0:
print("身高和体重必须大于 0,请重新输入。\n")
continue
break
except ValueError:
print("输入有误,请输入数字。\n")
bmi = calc_bmi(height, weight)
result = judge(bmi)
print("\n" + "-" * 30)
print(f"身高:{height} cm")
print(f"体重:{weight} kg")
print(f"BMI :{bmi}")
print(f"判定:{result}")
print("-" * 30)