>> 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)
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to