[Tutor] BMI Question
Prasad, Ramit
ramit.prasad at jpmorgan.com
Mon Sep 24 22:05:45 CEST 2012
Aija Thompson wrote:
> Hi!
>
> So I've been working on this question for hours! And Python keeps giving me an
> error saying that my syntax for BMI is wrong. I really can't understand why.
>
> So this is what I have so far:
One suggestion: immediately convert to appropriate number type
instead of storing the string values. It lets you know in the
appropriate spot if there was a problem with the input
instead of hiding it for later when you actually use the
values.
>
> w = raw_input('Please give your weight in lbs: ')
> h = raw_input('Now your height in feet and inches: ')
> h. split("'")
This line does nothing since the result of h.split("'")
> ft, inches = h. split("'")
ft = int(h.split("'")[0])
inches = int(h.split("'")[1].split('"')[0].strip())
weight = float(w) # or int?
> h_sum = float(12*int(ft)) + (int(inches.strip('"'))
Stephen already commented on your syntax error on the line above.
h_sum = 12.0 * ft + inches # The .0 will force float multiplication
> BMI = float(703*w)/float(h_sum*h_sum)
The above line fails because you never converted `w` to a number.
In addition, you do not need to convert everything to a float.
One float in the equation should mean that Python returns
the result of that equation as a float.
BMI = 703.0 * weight / h_sum**2
> print 'Your BMI is: ', BMI
>
> It's the first BMI that isn't agreeing with Python. Any help would do!
>
> Also, if there are any other serious problems, maybe you could let me know.
> I'm still having some trouble learning all of this.
>
> Thanks!
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Tutor
mailing list