Files

54 lines
1.3 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.
# -*- coding: utf-8 -*-
"""
BMI 计算器
输入:身高(厘米)、体重(公斤)
输出:BMI 值 + 按中国成人标准的体型判定
"""
def calculate_bmi(height_cm, weight_kg):
"""根据身高(cm)和体重(kg)计算 BMI 值"""
if height_cm <= 0:
raise ValueError("身高必须大于 0")
if weight_kg <= 0:
raise ValueError("体重必须大于 0")
height_m = height_cm / 100.0
return weight_kg / (height_m ** 2)
def classify(bmi):
"""按中国成人标准判断体型"""
if bmi < 18.5:
return "偏瘦"
elif bmi < 24.0:
return "正常"
elif bmi < 28.0:
return "超重"
else:
return "肥胖"
def print_result(height_cm, weight_kg):
bmi = calculate_bmi(height_cm, weight_kg)
print(f"身高:{height_cm} cm")
print(f"体重:{weight_kg} kg")
print(f"BMI:{bmi:.2f}")
print(f"体型:{classify(bmi)}")
def main():
"""交互式输入版本"""
try:
height = float(input("请输入身高(厘米,例如 165):"))
weight = float(input("请输入体重(公斤,例如 70):"))
except ValueError:
print("输入无效,请输入数字。")
return
print()
print_result(height, weight)
if __name__ == "__main__":
main()