if totalbmi <= 19:
print('Your bmi is {}, you are underweight'.format(totalbmi))
elif totalbmi >= 20 or totalbmi <= 28:
print('Your bmi is {} you are normal weight'.format(totalbmi))
else:
totalbmi >= 29
print('your bmi is {} you are overweight'.format(totalbmi))
返回:您的体重指数为55.4016620498615,您的体重正常。
很抱歉,这是一个愚蠢的初学者问题,但是我是一个愚蠢的初学者,在我看来,一切在逻辑上都是正确的。如果totalbmi是大于20或小于28的数字,则它将按预期方式返回“正常权重”。如果totalbmi是一个小于或等于19的数字,它将按预期方式再次返回“体重不足”。但是任何> = 29的东西似乎总是循环回到“您的体重正常”,而没有返回“您的体重超重”……应该吗?
预先感谢您的帮助
答案 0 :(得分:2)
应该为elif totalbmi >= 20 and totalbmi <= 28:
答案 1 :(得分:1)
您可以使用if-elif-else执行的逻辑:Python从上到下运行。这意味着它将从上到下检查条件,并在条件满足时中断。如果是这种情况,那么可以这样做:
if totalbmi > 28:
print('your bmi is {} you are overweight'.format(totalbmi))
elif totalbmi > 19:
print('Your bmi is {:} you are normal weight'.format(totalbmi))
else:
print('Your bmi is {}, you are underweight'.format(totalbmi))
答案 2 :(得分:0)
同意乔纳森(Jonathan),但最好的仍然是:
elif 20 <= totalbmi <= 28:
演示:
if totalbmi <= 19:
print('Your bmi is {}, you are underweight'.format(totalbmi))
elif 20 <= totalbmi <= 28:
print('Your bmi is {} you are normal weight'.format(totalbmi))
elif totalbmi >= 29:
print('your bmi is {} you are overweight'.format(totalbmi))
其他解决方案:
elif int(totalbmi) in range(20,29):
演示:
if totalbmi <= 19:
print('Your bmi is {}, you are underweight'.format(totalbmi))
elif int(totalbmi) in range(20,29):
print('Your bmi is {} you are normal weight'.format(totalbmi))
elif totalbmi >= 29:
print('your bmi is {} you are overweight'.format(totalbmi))
答案 3 :(得分:0)
同意或条件检查是否有任何条件为真。假设totalbmi = 66 因此达到elif totalbmi> = 66时,条件为true,这就是为什么它要打印elif的打印语句而从未到达else部分的原因。
在使用且totalbmi> = 66(条件为true)和totalbmi <= 66(条件为false)时,它会跳转到else部分并打印该语句