[Tutor] BMI calc

Peter Otten __peter__ at web.de
Wed Mar 13 17:36:36 CET 2013


Shall, Sydney wrote:

> On 13/03/2013 12:21, Peter Otten wrote:
>> Shall, Sydney wrote:
>>
>>> I am also a newbie, but I wish to learn, so I offer the following
>>> corrections.
>>> # Note. I do note know what units you are using.
>>> # I would use scientific, metric units. So you need to divide both
>>> weight and height by a suitable conversion factors.
>>> # Then you will not need the number 703.0, and the units in the second
>>> and third lines would be Kg/m. [Kilogram/Metre)
>>>
>>> def calc_BMI(weight,height):
>>>       bmi = (weight/(height*height))*703.0
>>>       print 'Your BMI is : ', BMI 'weight units/height units.' # You
>>>       need
>> to put the correct text here.
>>>       if bmi <=18.5:
>>>           print 'underweight'
>>>       if  18.5 <= bmi <= 24.9:
>>>           print 'normal weight'
>>>       if  25.0 <= bmi <= 29.9:
>>>           print 'overweight'
>>>       if bmi >= 30:
>>>           print 'obese'
>>>       return
>> A problem that I have not seen addressed yet:
>>
>> What will this print for the guy who is 75.0 in high and weighs 200.0 lb?
>>
>> His bmi is
>>
>>>>> height = 75.0
>>>>> weight = 200.0
>>>>> weight/(height*height)*703.0
>> 24.995555555555555
>>
>> That's neither normal nor overweight, according to your categorisation.

> Yes, you are right, of course.
> Thanks for reminding me that we are dealing with floats.
> I should have used approximations, with an (epsilon) error range.
> I will remember if future, I hope.

The solution I had in mind is simpler:

if bmi < 18.5:
    print "underweight"
elif bmi < 25:
    print "normal"
elif bmi < 30:
    print "overweight"
else:
    print "obese"

These give you effectively half-open intervals

18.5 <= bmi < 25 # normal
25 <= bmi < 30 # overweight

but each test relies on the result of the previous ones.



More information about the Tutor mailing list