jythonjes

How do I add the BMI calculation to the following code?


So heres my code below:

def inForm():
  name = requestString("What is your name?")
  age = requestInteger("What is your age?")
  height = requestInteger("What is your height?")
  weight = requestInteger("What is your weight?")
  print "Hello", name, "!", "You are", age,"years old!", "Your height  
  is", height, "cm", "and you weigh", weight, "kg"

I need to add the BMI calculation to this code, this is my formula, BMI =weight/(height*height)

With these messages displayed depending on BMI:

You are very severely underweight less than 15

Your are severely underweight from 15.0 to 16.0

You are underweight from 16.0 to 18.5

Your weight is normal from 18.5 to 25

You are overweight from 25 to 30

You are moderately obese from 30 to 35

You are severely obese from 35 to 40

You are very severely obese over 40

I can't seem to figure it out, how would i go by this?

Thanks in Advance!


Solution

  • Instead of converting from cm to m why don't you use requestNumber for the height and weight. Also, the print statement could be shorter like I have demonstrated below. Also, I use %s %d %r this is called String Formatting Operations.

    def inForm():
     name = requestString("Please enter your name")
     age = requestInteger("How old are you?")
     height = requestNumber("How tall are you? (Meters)")
     weight = requestNumber("How heavy are you? (Kilograms)")
     bmi = weight/(height*height)
    
     print "Hello, %s! I see that you are %r years old, %r meters tall, weight %r kgs and your BMI is %d." % (name, age, height, weight, bmi)
    
     n = bmi
     if n<15:
      print "You are very severely underweight"
     elif 15<=n<=16:
      print "You are severely underweight"
     elif 16<n<=18.5:
      print "You are underweight"
     elif 18.5<n<=25:
      print "Your weight is normal"
     elif 25<n<=30:
      print "You are overweight"
     elif 30<n<=35:
      print "You are moderately obese"
     elif 35<n<=40:
      print "You are severely obese"
     elif n>40:
      print "You are very severely obese"