443,660 Members | 1,105 Online Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

# How do I covert height from Feet (ex: 5'8") to inches in a BMI Calculator?

 P: 32 I am having trouble trying to figure out a way to convert the feet to inches and have this still work. i have ran into a brick wall pretty much and could use all the help i could get. The correct output code is suppose to look like this: Expand|Select|Wrap|Line Numbers What is your height (e.g., 5'8"): what is your weight (in pounds): Your BMI is...   So far my code looks like this, because i can not figure out a way to get it to convert and come out correctly. Expand|Select|Wrap|Line Numbers height = raw_input('''What is your height (e.g., 5'8"): ''') weight = raw_input('What is your weight (in pounds): ')   BMI = (703 * weight) / (height * height)   print 'Your BMI is %.4f'%(BMI)   raw_input()   any help would be greatly appreciated. thank you in advance! Sep 5 '10 #1

Use string method split() to separate the feet and inches. Example:
Expand|Select|Wrap|Line Numbers
1. >>> height = "5'8\""
2. >>> height
3. '5\'8"'
4. >>> height.split("'")
5. ['5', '8"']
6. >>> ft, inches = height.split("'")
7. >>> 12*int(ft) + int(inches.strip('"'))
8. 68
9. >>>

6 Replies

 Expert Mod 2.5K+ P: 2,851 Use string method split() to separate the feet and inches. Example: Expand|Select|Wrap|Line Numbers >>> height = "5'8\"" >>> height '5\'8"' >>> height.split("'") ['5', '8"'] >>> ft, inches = height.split("'") >>> 12*int(ft) + int(inches.strip('"')) 68 >>>  Sep 5 '10 #2

 P: 32 Ok thank you for the help, the formula works like it should, but i cant get it to work and calculate the BMI. Expand|Select|Wrap|Line Numbers height = raw_input('''What is your height (e.g., 5'8"): ''') weight = raw_input('What is your weight (in pounds): ')   height.split("'")   ft, inches = height.split("'")   height = 12 * int(ft) + int(inches.strip('"'))   BMI = (703 * weight) / (height * height)   print 'Your BMI is%.4f'%(BMI)   raw_input()   I get the following error: Expand|Select|Wrap|Line Numbers Traceback (most recent call last):   File "E:\CSIS\Projects\ex2_34.py", line 16, in      BMI = (703 * weight) / (height * height) TypeError: unsupported operand type(s) for /: 'str' and 'int'   what am i doing wrong? Sep 5 '10 #3

 Expert Mod 2.5K+ P: 2,851 raw_input() returns a string. Type cast the value to float as in: Expand|Select|Wrap|Line Numbers >>> weight = "150" >>> float(weight) 150.0 >>>  Sep 5 '10 #4

 P: 32 I'm not quite understanding where this is suppose to go... this is my input: Expand|Select|Wrap|Line Numbers height = raw_input('''What is your height (e.g., 5'8"): ''') weight = raw_input('What is your weight (in pounds): ')   height.split("'")   ft, inches = height.split("'")   height_sum = 12 * int(ft) + int(inches.strip('"'))   BMI = float(703 * weight) / float(height_sum * height_sum)   print 'Your BMI is %.2f'%(BMI)   raw_input()   and this is my result: Expand|Select|Wrap|Line Numbers What is your height (e.g., 5'8"): 5'10" What is your weight (in pounds): 140 Your BMI is 1.\$   I am a bit new to this so im not sure exactly what to do :/ Sep 5 '10 #5

 Expert Mod 2.5K+ P: 2,851 See if this helps: Expand|Select|Wrap|Line Numbers >>> float(703*'140') 1.#INF >>> weight = '140' >>> 2*weight '140140' >>> float(2*weight) 140140.0 >>> 2*float(weight) 280.0 >>>  Can you see where you went wrong? Sep 5 '10 #6

 P: 32 :D yes! i understand now. will that happen every time i have it setup that way? thank you so much for the help! Sep 5 '10 #7 