By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
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
  1. What is your height (e.g., 5'8"):
  2. what is your weight (in pounds):
  3. Your BMI is...
  4.  
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
  1. height = raw_input('''What is your height (e.g., 5'8"): ''')
  2. weight = raw_input('What is your weight (in pounds): ')
  3.  
  4. BMI = (703 * weight) / (height * height)
  5.  
  6. print 'Your BMI is %.4f'%(BMI)
  7.  
  8. raw_input()
  9.  
any help would be greatly appreciated. thank you in advance!
Sep 5 '10 #1

✓ answered by bvdet

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. >>> 

Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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. >>> 
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
  1. height = raw_input('''What is your height (e.g., 5'8"): ''')
  2. weight = raw_input('What is your weight (in pounds): ')
  3.  
  4. height.split("'")
  5.  
  6. ft, inches = height.split("'")
  7.  
  8. height = 12 * int(ft) + int(inches.strip('"'))
  9.  
  10. BMI = (703 * weight) / (height * height)
  11.  
  12. print 'Your BMI is%.4f'%(BMI)
  13.  
  14. raw_input()
  15.  
I get the following error:
Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "E:\CSIS\Projects\ex2_34.py", line 16, in <module>
  3.     BMI = (703 * weight) / (height * height)
  4. TypeError: unsupported operand type(s) for /: 'str' and 'int'
  5.  
what am i doing wrong?
Sep 5 '10 #3

bvdet
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
  1. >>> weight = "150"
  2. >>> float(weight)
  3. 150.0
  4. >>> 
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
  1. height = raw_input('''What is your height (e.g., 5'8"): ''')
  2. weight = raw_input('What is your weight (in pounds): ')
  3.  
  4. height.split("'")
  5.  
  6. ft, inches = height.split("'")
  7.  
  8. height_sum = 12 * int(ft) + int(inches.strip('"'))
  9.  
  10. BMI = float(703 * weight) / float(height_sum * height_sum)
  11.  
  12. print 'Your BMI is %.2f'%(BMI)
  13.  
  14. raw_input()
  15.  
and this is my result:
Expand|Select|Wrap|Line Numbers
  1. What is your height (e.g., 5'8"): 5'10"
  2. What is your weight (in pounds): 140
  3. Your BMI is 1.$
  4.  
I am a bit new to this so im not sure exactly what to do :/
Sep 5 '10 #5

bvdet
Expert Mod 2.5K+
P: 2,851
See if this helps:
Expand|Select|Wrap|Line Numbers
  1. >>> float(703*'140')
  2. 1.#INF
  3. >>> weight = '140'
  4. >>> 2*weight
  5. '140140'
  6. >>> float(2*weight)
  7. 140140.0
  8. >>> 2*float(weight)
  9. 280.0
  10. >>> 
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

Post your reply

Sign in to post your reply or Sign up for a free account.