[Tutor] BMI Question

eryksun eryksun at gmail.com
Mon Sep 24 22:10:56 CEST 2012

```On Mon, Sep 24, 2012 at 3:13 PM, Aija Thompson <aijathompson at hotmail.com> wrote:
>
> ft, inches = h. split("'")
> h_sum = float(12*int(ft)) + (int(inches.strip('"'))

Leave h_sum as an int:

h_sum = 12 * int(ft) + int(inches.strip('"'))

A better name here would be "h_inch". Also, make sure your parentheses
are closed. You have an extra left parenthesis in your expression. A
good editor will highlight matching parentheses.

> BMI = float(703*w)/float(h_sum*h_sum)

w is a string. Multiplying a string by N gives you a new string
containing N copies. For example, 703 copies of "180" is a number
string beyond the range of a float, so the result is inf (infinity).
You need to convert w to an int before multiplying. Also, the
expression is evaluated from left to right (based on standard order of
operations) and will return a float if you start with the constant
703.0. For example 703.0 * 180 / (72 * 72) == 24.40972222222222.

Here's the revised expression:

BMI = 703.0 * int(w) / (h_sum * h_sum)

Finally, you can round the BMI before printing, say to 2 decimal places:

print 'Your BMI is:', round(BMI, 2)

But using string formatting would be better:

print 'Your BMI is: %.2f' % BMI
```