體重指數計算器:
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中文網其他相關文章!