[Tutor] BMI calc
Albert-Jan Roskam
fomcl at yahoo.com
Wed Mar 13 18:03:56 CET 2013
>> 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"
How about this approach? the "if" statements are simpler=better, but this is fun too!
import bisect
def getBMI(height, weight, cutoffs=[18, 25, 30],
categories=('under', 'normal', 'obese', 'morbidly')):
bmi = weight / float(height ** 2)
return categories[bisect.bisect(cutoffs, bmi)]
cutoffs=[18, 25, 30]
categories =('skinny', 'normal', 'teddybear', 'huge teddybear')
print getBMI(1.82, 180, cutoffs, categories)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130313/cc177372/attachment.html>
More information about the Tutor
mailing list