Liz S - 1 year ago 235
Python Question

Calculating BMI through Python: Take 2

Sorry about my previous question. The question I have to answer is this:

Body Mass Index (BMI) is a good indicator of body fatness for most people. The formula for BMI is weight/ height2 where weight is in kilograms and height is in meters. Write a program that prompts for weight in pounds and height in inches, converts the values to metric, and then calculates and displays the BMI value.

What I have so far is this:

``````"""
BMI Calculator

1. Obtain weight in pounds and height in inches
2. Convert weight to kilograms and height to meters
3. Calculate BMI with the formula weight/height^2
"""

#prompt user for input from keyboard
weight= input ("How much do you weigh (in pounds)?")
#this get's the person's weight in pounds

weight_in_kg= weight/2.2
#this converts weight to kilograms

height= input ("What is your height (in inches)?")
#this gets the person's height in inches

height_in_meter=height*2.54
#this converts height to meters

bmi=weight_in_kg/(height_in_meters*2)
#this calculates BMI

print (BMI)
``````

My first step works, but that is the easy part. I know that in Python the equal sign assigns something, so I'm not sure if that is the problem, but I really do not know what to do. I am really sorry. When I run the program it says:

TypeError : unsupported operand type(s) for /: 'str' and 'float'

If anyone could give me any tips on what I am doing wrong, I would really appreciate it. And if not, thank you for your time. Thanks again.

For Python 3.x (as specified):

The problem is that keyboard input from `input()` is of type `str` (string), which is not a numeric type (even if the user types in numbers). However, this is easily fixed by changing the input lines like so:

``````weight = float(input("How much do you weigh (in pounds)?"))
``````

and

``````height = float(input("What is your height (in inches)?"))
``````

thus converting the string output of `input()` into the numeric `float` type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download