通过Python计算体重指数:取2

通过Python计算体重指数:取2,python,Python,对不起,我刚才的问题。我要回答的问题是: 身体质量指数(BMI)是大多数人身体肥胖的良好指标。BMI的公式为体重/身高2,其中体重单位为千克,身高单位为米。编写一个程序,提示输入以磅为单位的体重和以英寸为单位的身高,将值转换为公制,然后计算并显示BMI值 到目前为止,我得到的是: """ BMI Calculator 1. Obtain weight in pounds and height in inches 2. Convert weight to kilograms and height

对不起,我刚才的问题。我要回答的问题是:

身体质量指数(BMI)是大多数人身体肥胖的良好指标。BMI的公式为体重/身高2,其中体重单位为千克,身高单位为米。编写一个程序,提示输入以磅为单位的体重和以英寸为单位的身高,将值转换为公制,然后计算并显示BMI值

到目前为止,我得到的是:

"""
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)
我的第一步是可行的,但这是最简单的部分。我知道在Python中,等号赋值,所以我不确定这是否是问题所在,但我真的不知道该怎么办。我真的很抱歉。当我运行程序时,它会说:

TypeError:/:“str”和“float”的操作数类型不受支持

如果有人能告诉我我做错了什么,我会非常感激。如果没有,谢谢你抽出时间。再次感谢。

对于Python3.x(如指定):

问题是来自
input()
的键盘输入类型为
str
(字符串),而不是数字类型(即使用户键入数字)。但是,可以通过如下方式更改输入行轻松解决此问题:

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


因此,将
input()
的字符串输出转换为数字
float
类型。

首先,输入错误:
height(以米为单位)
。 对于Python2,前面的
浮点(…
是不必要的,尽管我确信这是一个很好的实践

"""
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)

我发现这是最简单的方法,希望这有帮助

你的问题是你试图用一个
float
乘一个字符串
str
这应该可以解决问题。编辑-答案为@ezod.Yes.Python3.2 for Windows。在Python3中,
input
返回一个
str
。您可以使用
weight=float(输入(“您的体重(磅)是多少”)
以确保将体重转换为浮点数等。顺便说一句,我想你指的是
身高,单位为米**2
——即身高平方,而不是身高乘以2。值得注意的是Python区分大小写(例如,BMI和BMI不相同)所以print语句中的变量应该是bmi。值得注意的是,在2.x中这不是真的…应该尽可能避免输入…谢谢!这有点帮助!
# 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)
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)
# 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)