fork download
  1. def calculate_bmi(weight, height):
  2. """计算BMI指数"""
  3. if height <= 0:
  4. raise ValueError("身高必须是正数")
  5. if weight < 0:
  6. raise ValueError("体重不能为负数")
  7. return weight / (height ** 2)
  8.  
  9. def get_bmi_category(bmi):
  10. """根据BMI指数返回对应的健康类别"""
  11. if bmi < 18.5:
  12. return "偏瘦"
  13. elif 18.5 <= bmi < 24:
  14. return "正常"
  15. elif 24 <= bmi < 28:
  16. return "过重"
  17. elif 28 <= bmi < 30:
  18. return "轻度肥胖"
  19. elif 30 <= bmi < 35:
  20. return "中度肥胖"
  21. else:
  22. return "重度肥胖"
  23.  
  24. def main():
  25. try:
  26. # 获取用户输入
  27. weight = float(input("请输入您的体重(kg):30 "))
  28. height = float(input("请输入您的身高(m): 138"))
  29.  
  30. # 计算BMI
  31. bmi = calculate_bmi(weight, height)
  32.  
  33. # 获取健康类别
  34. category = get_bmi_category(bmi)
  35.  
  36. # 输出结果
  37. print(f"\n您的BMI指数为: {bmi:.2f}")
  38. print(f"健康类别: {category}")
  39.  
  40. except ValueError as e:
  41. print(f"输入错误: {e}")
  42. except Exception as e:
  43. print(f"发生错误: {e}")
  44.  
  45. if __name__ == "__main__":
  46. main()
Success #stdin #stdout 0.08s 14112KB
stdin
Standard input is empty
stdout
请输入您的体重(kg):30 发生错误: EOF when reading a line