体重指数计算器:
BMI 是一种快速且廉价的方法,可将一个人的体重分为体重不足、正常、超重或肥胖。
BMI 公式:
BMI 的计算方法是体重除以身高的平方:
公制单位:
BMI = 体重 (kg) / [身高 (m)]2
美国习惯单位:
BMI = 体重(磅)/[身高(英寸)]2 x 703
示例:
# Input the weight in kilograms weight = float(input("Enter your weight (in kg): ")) # Input the height in meters height = float(input("Enter your height (in meters): ")) # Calculate BMI using the formula: bmi = weight / (height ** 2) # Output the calculated BMI value print("BMI:", round(bmi, 2)) # Rounds BMI to 2 decimal places # Function to provide feedback based on BMI def bmi_feedback(bmi): if bmi < 18.5: return "You are underweight" elif 18.5 <= bmi <= 24.9: return "You have a healthy weight" elif 25 <= bmi <= 29.9: return "You are overweight" else: return "You are obese" # Call the feedback function and store the result bmi_result = bmi_feedback(bmi) # Output the BMI category feedback print("BMI Result:", bmi_result)
输出:
Enter your weight:58 Enter your height:1.67 BMI: 20.796729893506402 BMI Result: You have a healthy weight
以上是编写 BMI 计算器的 Python 程序的详细内容。更多信息请关注PHP中文网其他相关文章!