3

对不起我之前的问题。我必须回答的问题是:

对于大多数人来说,体重指数 (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'

如果有人能给我任何关于我做错了什么的提示,我将不胜感激。如果没有,感谢您抽出宝贵时间。再次感谢。

4

4 回答 4

4

对于 Python 3.x(如指定):

问题是键盘输入input()是类型str(字符串),它不是数字类型(即使用户输入数字)。但是,这很容易通过更改输入行来解决,如下所示:

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

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

从而将字符串输出input()转换为数字float类型。

于 2012-11-21T00:39:06.310 回答
2

首先,有一个错字:height_in_meter(S). 对于 Python 2,前面float(...不是必需的,尽管我确信这是一个很好的做法。

"""
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)
于 2013-01-25T23:14:40.313 回答
0
# 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)
于 2016-08-03T11:45:33.173 回答
0
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)

我发现这是使其工作的最简单方法,希望这会有所帮助

于 2015-07-26T12:12:05.140 回答