我正在計算Python 3中某個人的BMI,並且需要檢查BMI是否在兩個值之間。使用Python檢查變量是否在兩個值之間
這是我的代碼:
def metricBMI():
text = str('placeholder')
#Get height and weight values
height = float(input('Please enter your height in meters: '))
weight = float(input('Please enter your weight in kilograms: '))
#Square the height value
heightSquared = (height * height)
#Calculate BMI
bmi = weight/heightSquared
#Print BMI value
print ('Your BMI value is ' + str(bmi))
if bmi < 18:
text = 'Underweight'
elif 24 >= bmi and bmi >= 18:
text = 'Ideal'
elif 29 >= bmi and bmi >= 25:
text = 'Overweight'
elif 39 >= bmi and bmi >= 30:
text = 'Obese'
elif bmi > 40:
text = 'Extremely Obese'
print ('This is: ' + text)
這將輸出減持完美的罰款,但其他人一樣非常不定義文本。
輸出:
Calulate BMI, BMR or Harris Benedict Equation (HBE) or exit? bmi
Do you work in metric (M) or imperial (I)m
Please enter your height in meters: 1.8
Please enter your weight in kilograms: 80
Your BMI value is 24.691358024691358
This is: placeholder
我猜有什麼問題,我檢查變量,但我無法看到它的方式。
感謝,
傑克
請修復代碼縮進。 –
你應該用[Ashalynd](http://stackoverflow.com/a/33035965/2723675)回答你的用例,但如果有人根據標題在這個頁面上絆倒了,你可以檢查一個變量是否是在兩個數字之間像這樣:'if min
iLoveTux