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))
回来了:你的bmi是55.4016620498615你是正常体重。
抱歉这个明显愚蠢的初学者问题,但我是一个愚蠢的初学者,一切看起来在逻辑上对我来说都是正确的。如果totalbmi是一个大于20或小于28的数字,它将返回“正常重量”。如果totalbmi是一个小于或等于19的数字,它会再次按照预期返回“体重不足”。但是任何> = 29总是似乎循环回到“你是正常的体重”并且没有回归“你超重”......它应该如何?
在此先感谢您的帮助
应该是elif totalbmi >= 20 and totalbmi <= 28:
您可以使用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))
同意乔纳森,但最好还是:
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))
同意或条件检查是否有任何条件成立。假设totalbmi = 66所以达到elif totalbmi> = 66因此条件为真,这就是为什么它打印了elif的打印语句而从未到达其他部分。
使用和totalbmi> = 66(条件为真)和totalbmi <= 66(条件为假)所以它跳转到else部分并打印该语句