我在Python 3中计算一个人的BMI,需要检查BMI是否介于两个值之间.
这是我的代码:
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)
Run Code Online (Sandbox Code Playgroud)
这将输出Underweight非常好,但像Ideal这样的其他人不会定义文本.
输出:
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
Run Code Online (Sandbox Code Playgroud)
我猜测我检查变量的方式有问题,但我看不到它.
谢谢,
可靠的人
您的BMI不属于您的任何条件(超过24且低于25,并且您的情况不包括在内).
事实上,你可以像这样简化你的条件:
if bmi < 18:
text = 'Underweight'
elif bmi <= 24: # we already know that bmi is >=18
text = 'Ideal'
elif bmi <= 29:
text = 'Overweight'
elif bmi <= 39:
text = 'Obese'
else:
text = 'Extremely Obese'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9083 次 |
| 最近记录: |