0

I am working on a BMI calculator and have a bunch of if statements for the "status" part of it. For some reason I am getting an error through Eclipse saying that "Expected:)" but I have no clue what is missing.

Here is a sample of the code which is throwing the error:

BMI = mass / (height ** 2)

if(BMI < 18.5):
    status = "Underweight"

if(BMI => UNDERWEIGHT and BMI < NORMAL):
    status = "Normal"

if(BMI => NORMAL & BMI < OVERWEIGHT):
    status = "Overweight"

elif(BMI >= 30):
    status = "Obese"
4

4 回答 4

4

正如在其他答案中已经指出的那样,错误是由 , 引起的=>,并且&按位运算符,在这种情况下这不是您想要的。但根据@Blckknght 的评论,无论如何您都可以通过每次仅与最大值进行比较来简化这一点。另外,去掉括号,因为 Python 不需要这些括号。

BMI = mass / (height ** 2)    
if BMI < UNDERWEIGHT:
    status = "Underweight"
elif BMI < NORMAL:
    status = "Normal"
elif BMI < OVERWEIGHT:
    status = "Overweight"
else:
    status = "Obese"
于 2013-10-14T01:40:21.653 回答
3

=> does not mean anything in Python. "Greater than or equal to" is instead written >=.

于 2013-10-14T00:53:57.197 回答
2

You might change:

if(BMI => NORMAL & BMI < OVERWEIGHT):

to:

if(BMI >= NORMAL and BMI < OVERWEIGHT):

With some of the other suggestions, you might re-write the entire statement as:

if BMI < UNDERWEIGHT:
    status = "Underweight"

elif BMI >= UNDERWEIGHT and BMI < NORMAL:
    status = "Normal"

elif BMI >= NORMAL and BMI < OVERWEIGHT:
    status = "Overweight"

elif BMI >= OVERWEIGHT:
    status = "Obese"
于 2013-10-14T00:54:40.927 回答
0
BMI = mass / (height ** 2)

if (BMI < 18.5):
    status = "Underweight"
elif (UNDERWEIGHT < BMI < NORMAL):
    status = "Normal"
elif (NORMAL < BMI < OVERWEIGHT):
    status = "Overweight"
else
    status = "Obese"

在python中,我们可以检查一个数字是否在范围内,像这样

if 0 < MyNumber < 2:

只有当 MyNumber 是 0 到 2 之间的某个数字时,这才是真实的。

于 2013-10-14T01:19:35.490 回答