[Tutor] BMI calc

Alan Gauld alan.gauld at btinternet.com
Wed Mar 13 09:36:05 CET 2013


On 13/03/13 03:07, Soliman, Yasmin wrote:
> I'm using python 2.7,

Then let us see the syntax errors because otherwise we are guessing.

 > where exactly does the return statment ... get posted in the function?

I'm not sure what you mean by 'get posted'
It is activated once the function is called.

ie

def foo(x):
    return 2 + x

does nothing until I call it:

result = foo(5)
print result

> def calc_BMI(weight,height):
>      if bmi <=18.5:
>          return 'underweight'

Since BMI is a value your function is better as it was, just returning 
the value.

The code to convert that BMI value into a weight assessment is better 
done outside, again as you had before. (Or inside another function, 
rateBMI(bmi) maybe)

What was missing was some glue that joins those 2 things together:

read weight and height
call CalcBMI
use result to assess weight.

>      elif bmi >= 18.5 and bmi <=24.9:
>          return 'normal weight'

BTW in Python you can abbreviate the tests slightly with

elif 18.5 <= bmi <= 24.9:
    print 'normal weight'

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list