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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuugie
    New Member
    • Sep 2010
    • 32

    #1

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

    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:
    Code:
    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.
    Code:
    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!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Use string method split() to separate the feet and inches. Example:
    Code:
    >>> height = "5'8\""
    >>> height
    '5\'8"'
    >>> height.split("'")
    ['5', '8"']
    >>> ft, inches = height.split("'")
    >>> 12*int(ft) + int(inches.strip('"'))
    68
    >>>

    Comment

    • Fuugie
      New Member
      • Sep 2010
      • 32

      #3
      Ok thank you for the help, the formula works like it should, but i cant get it to work and calculate the BMI.

      Code:
      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:
      Code:
      Traceback (most recent call last):
        File "E:\CSIS\Projects\ex2_34.py", line 16, in <module>
          BMI = (703 * weight) / (height * height)
      TypeError: unsupported operand type(s) for /: 'str' and 'int'
      what am i doing wrong?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        raw_input() returns a string. Type cast the value to float as in:
        Code:
        >>> weight = "150"
        >>> float(weight)
        150.0
        >>>

        Comment

        • Fuugie
          New Member
          • Sep 2010
          • 32

          #5
          I'm not quite understanding where this is suppose to go...

          this is my input:
          Code:
          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:
          Code:
          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 :/

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            See if this helps:
            Code:
            >>> 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?

            Comment

            • Fuugie
              New Member
              • Sep 2010
              • 32

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

              Comment

              Working...