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"
1
  • Where do you see this error Commented Oct 14, 2013 at 0:53

4 Answers 4

4

As already noted on other answers, the error is caused by =>, and & is a bitwise operator which is not what you want in this context. But as per @Blckknght's comment, you can probably simplify this anyway by only comparing to the maximum value each time. Also, get rid of the parentheses as these are not needed in Python.

BMI = mass / (height ** 2)    
if BMI < UNDERWEIGHT:
    status = "Underweight"
elif BMI < NORMAL:
    status = "Normal"
elif BMI < OVERWEIGHT:
    status = "Overweight"
else:
    status = "Obese"
Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Its worth noting that if you used a elif for the second and third if statements, and an else for the last one, you wouldn't need to do any >= tests at all. Another issue is the & in the third test.
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"

1 Comment

you may want to consider getting rid of ( and ) as well.
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"

In python we can check whether a number is in the range, like this

if 0 < MyNumber < 2:

This will be Truthy only when MyNumber is some number between 0 and 2.

1 Comment

I think the OP wants <= rather than < in several places

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.