Liz S
Liz S

Reputation: 37

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.

Upvotes: 3

Views: 14495

Answers (4)

RishiKesh Pathak
RishiKesh Pathak

Reputation: 2272

# height and weight are available as a regular lists

# Import numpy
import numpy as np

# Create array from height with correct units: np_height_m
np_height_m = np.array(height) * 0.0254

# Create array from weight with correct units: np_weight_kg 
np_weight_kg = np.array(weight) * 0.453592

# Calculate the BMI: bmi
bmi =  np_weight_kg / (np_height_m **2)

# Print out bmi
print(bmi)

Upvotes: 0

user5157441
user5157441

Reputation: 1

weight= float(input("How much do you weigh (in kg)?"))
weight_in_kg= float(input("Enter weight in kg?"))


#height= float(input("What is your height (in meters)?"))
height_in_meter= float(input("Enter height in metres?"))


bmi=weight_in_kg/(height_in_meter**2)


print (bmi)

I found this was the easiest way to make it work, Hope this helps

Upvotes: 0

Tigerider
Tigerider

Reputation: 121

First off, there was a typo: height_in_meter(S). For Python 2, a preceding float(... is not necessary, although I am sure it is good practice.

"""
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/100
#this converts height to centimeters

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

print (bmi)

Upvotes: 2

ezod
ezod

Reputation: 7421

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.

Upvotes: 4

Related Questions