0

Not sure what is wrong with this code, I'm just starting to use Python 2.7, and have run into a problem with this bmi calculator script.

def bmi_calculator():

    print "Enter your appelation (Mr., Mrs., Dr., ...): "
    appelation = raw_input()
    print "Enter your first name: "
    fname = raw_input()
    print "Enter your last name: "
    lname = raw_input()
    print "Enter your height in inches: "
    height = raw_input()
    print "Enter your weight in pounds: "
    weight = raw_input()

    feet = (height/12)
    inches = (height-feet)*12
    bmi = ((weight/(height*height))*703)

    print "BMI Record for %s %s %s:" % (appelation,fname,lname)
    print "Subject is %d feet %d inches tall and weighs %d pounds" %    (feet,inches,weight)
    print "Subject's BMI is %d" % (bmi)

Would someone mind telling me what I am doing wrong?


This is the error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "a1.py", line 88, in bmi_calculator
feet = (height/12)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
2
  • What exactly is the problem? Commented Sep 11, 2013 at 1:00
  • 5
    You need to convert the inputs to numbers, by default they are strings. Commented Sep 11, 2013 at 1:01

1 Answer 1

4

You need to cast from str to a numeric type, such as float:

def bmi_calculator():
    print "Enter your appelation (Mr., Mrs., Dr., ...): "
    appelation = raw_input()
    print "Enter your first name: "
    fname = raw_input()
    print "Enter your last name: "
    lname = raw_input()
    print "Enter your height in inches: "
    height = float(raw_input())
    print "Enter your weight in pounds: "
    weight = float(raw_input())
    feet = float((height/12))
    inches = (height-feet)*12
    bmi = ((weight/(height*height))*703)
    print "BMI Record for %s %s %s:" % (appelation,fname,lname)
    print "Subject is %d feet %d inches tall and weighs %d pounds" %    (feet,inches,weight)
    print "Subject's BMI is %d" % (bmi)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.