7 Replies - 2610 Views - Last Post: 03 July 2011 - 11:07 AM Rate Topic: -----

#1 Smokin2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Else If statement question

Posted 24 June 2011 - 12:52 PM

if is lower cased.
Was This Post Helpful? 2
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2085
  • View blog
  • Posts: 3,171
  • 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

Was This Post Helpful? 2
  • +
  • -

#4 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • 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()

print "Your BMI is", str(bmi)

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

Was This Post Helpful? 2
  • +
  • -

#5 Smokin2k  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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'
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2085
  • View blog
  • Posts: 3,171
  • 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.
Was This Post Helpful? 4
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • 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)).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1