编写程序,根据身高和体重计算BMI值,同时输出国际和国内BMI指标建议值所确定的指标分类信息。用python实现
时间: 2024-09-22 14:03:52 浏览: 44
Python–根据计算公式:BMI = 体重(kg) / 身高(m)^2判断身体状况
5星 · 资源好评率100%
在Python中,我们可以编写一个简单的函数来计算BMI(身体质量指数),并根据标准值判断属于正常、偏瘦、偏胖还是肥胖。这里是一个基本的例子:
```python
def calculate_bmi(height_cm, weight_kg):
# 将身高从厘米转换为米
height_m = height_cm / 100.0
# 计算BMI
bmi = weight_kg / (height_m ** 2)
# 定义国际和中国的BMI分类标准
international_thresholds = {
"Underweight": (18.5, 24.9),
"Normal": (25, 29.9),
"Overweight": (30, 34.9),
"Obese": (>=35)
}
chinese_thresholds = {
"过轻": (18.5, 23.9),
"正常": (24, 27.9),
"超重": (28, 29.9),
"肥胖": (>=30)
}
# 根据中国和国际的标准检查BMI
international_status = ""
if bmi < international_thresholds["Underweight"][0]:
international_status = "Underweight"
elif bmi >= international_thresholds["Underweight"][1] and \
bmi < international_thresholds["Normal"][0]:
international_status = "Normal"
elif bmi >= international_thresholds["Normal"][1] and \
bmi < international_thresholds["Overweight"][0]:
international_status = "Overweight"
else:
international_status = "Obese"
chinese_status = ""
if bmi < chinese_thresholds["过轻"][0]:
chinese_status = "过轻"
elif bmi >= chinese_thresholds["过轻"][1] and \
bmi < chinese_thresholds["正常"][0]:
chinese_status = "正常"
elif bmi >= chinese_thresholds["正常"][1] and \
bmi < chinese_thresholds["超重"][0]:
chinese_status = "超重"
else:
chinese_status = "肥胖"
return bmi, f"International: {international_status}, Chinese: {chinese_status}"
# 测试函数
height = float(input("请输入您的身高(cm): "))
weight = float(input("请输入您的体重(kg): "))
bmi, classification = calculate_bmi(height, weight)
print(f"Your BMI is: {bmi:.2f}")
print(f"BMI Classification: {classification}")
阅读全文