# Else If statement question

Page 1 of 1

## 7 Replies - 7015 Views - Last Post: 03 July 2011 - 11:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=236913&amp;s=ce1a714bb669915d6e814b6009f352b7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Smokin2k

Reputation: 0
• Posts: 3
• Joined: 24-June 11

# Else If statement question

Posted 24 June 2011 - 12:48 PM

Hi Im trying to write a program that calculates a persons body mass index and then determines whether the person is overweight, underweight or optimal weight. The first part to calculate the BMI is working, but when I added the Else IF statements for some reason it is not recognising the original BMI. It says there is a syntax error. Also I am a total noob to python and programming in general. Here is the code

```# The main function
def main():
weight = input("Please enter weight: ")
height = input("Please enter height: ")
bmi = weight * (703.0 / (height * height))
printOutput(bmi)

# This function does the math for the conversion and prints the output to the user.
def printOutput(index):
print "BMI = " + str(index)

If bmi < 18.5:
print "You are considered to be underweight."
else:
If bmi > 25:
print "You are considered to be overweight."
else:
If bmi <= 25 and bmi >= 18.5:
print "Your weight is considered to be optimal!"

main()

```

Is This A Good Question/Topic? 0

## Replies To: Else If statement question

### #2 Motoma

Reputation: 452
• Posts: 798
• Joined: 08-June 10

## Re: Else If statement question

Posted 24 June 2011 - 12:52 PM

if is lower cased.

### #3 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: Else If statement question

Posted 24 June 2011 - 12:53 PM

if is spelled with a lower case i. Also you need to indent the print statement that comes after your first if. Further else should be indented at the same depth as the corresponding if.

You should also use elif instead of else followed by if to reduce nesting.

This post has been edited by sepp2k: 24 June 2011 - 12:54 PM

### #4 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

## Re: Else If statement question

Posted 24 June 2011 - 12:54 PM

There are two problems that I see.

1) Those If's should be if's (or can you use If in Python?) (Sorry for beating that dead horse.)
2) bmi is defined inside main(), but not outside of main(). You are trying to call a variable that doesn't exist. I recommend you try something more like this:

```def calculateBMI():
weight = raw_input("Please enter weight: ")
height = raw_input("Please enter height: ")

return weight * (703.0 / (height * height))

bmi = calculateBMI()

if bmi < 18.5:
print "You are underweight."
elif bmi > 25:
print "You are overweight."
else:
print "You are of normal weight."
```

EDIT:

Quote

You should also use elif instead of else followed by if to reduce nesting.

In this case, I would agree. However, I think it is important to note that because of how if-statements work, this isn't always the way that you should do things.

When Python processes an if-statement, it looks for the first True condition, and then stops. It doesn't matter whether anything after that point is True, because it's not going to be processed. I use nested loops to create a hierarchy.

For example:

```      // Check for neeed to change direction.
if (backUp > 0) { // Robot needs to back up, back up

speed[0] = -SPEED;
speed[1] = -SPEED;
backUp--;

} else { // Robot does not need to back up, check need to turn.

if (leftTurn > 0) { // Robot needs to turn left, turn left
speed[0] = -SPEED / 2;
speed[1] = SPEED / 2;
leftTurn--;
}

if (rightTurn > 0) { // Robot needs to turn right, turn right
speed[0] = SPEED / 2;
speed[1] = -SPEED / 2;
rightTurn--;
}
}
```

In this block of code I first check to see if the robot has decided that it needs to back up. I've determined that backing up is more important than turning, so this comes first. If we don't need to back up then I want to check whether it is necessary to turn left or right. In this case, the needs to turn left and to turn right are of equal importance, so I use two if-statements instead of one if-else.

This post has been edited by Brewer: 24 June 2011 - 01:01 PM

### #5 Smokin2k

Reputation: 0
• Posts: 3
• Joined: 24-June 11

## Re: Else If statement question

Posted 24 June 2011 - 01:05 PM

Thanks guys! and thanks for catching that Brewer

hmm actually that code has an error too Brewer, Its coming back with:

Traceback (most recent call last):
File "C:\Users\User\Desktop\Programming Assignments\Ch4ex10.py", line 15, in <module>
bmi = calculateBMI()
File "C:\Users\User\Desktop\Programming Assignments\Ch4ex10.py", line 13, in calculateBMI
return weight * (703.0 / (height * height))
TypeError: can't multiply sequence by non-int of type 'str'

### #6 sepp2k

• D.I.C Lover

Reputation: 2620
• Posts: 4,175
• Joined: 21-June 11

## Re: Else If statement question

Posted 24 June 2011 - 01:09 PM

raw_input returns strings, so you need to call int() on the strings returned by raw_input to turn them into integers when you assign weight and height.

### #7 atraub

• Pythoneer

Reputation: 828
• Posts: 2,236
• Joined: 23-December 08

## Re: Else If statement question

Posted 24 June 2011 - 01:45 PM

Wow you guys responded fast, I couldn't even get the bat off my shoulder on this one.

### #8 Brewer

• Awesome

Reputation: 182
• Posts: 1,044
• Joined: 14-June 10

## Re: Else If statement question

Posted 03 July 2011 - 11:07 AM

@Smokin2k - It just came to my attention that your formula is wrong. The correct equation is (weight * 703) / (height * height), not weight / (703 * (height * height)).