python - 通过Python计算BMI : Take 2

标签 python

抱歉我之前的问题。我要回答的问题是:

对于大多数人来说, body 质量指数 (BMI) 是衡量 body 肥胖程度的良好指标。 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”

如果有人能就我做错了什么给我任何提示,我将不胜感激。如果没有,谢谢你的时间。再次感谢。

最佳答案

对于 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类型。

关于python - 通过Python计算BMI : Take 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13484573/

相关文章:

python - 在 tensorflow 中使用迭代器生成特征和标签

python - Python 上的 Azure Face API 中人脸验证的错误代码请求

python - 在 Python 中获取彩色形状周围边界的坐标

python - 在 numpy 数组中查找 False-True 转换

python - 如何从pytest中获取通过、失败和跳过的测试总数

python - PyQt5 QtSql 在 QThread 中访问数据库

python - 如何在 matplotlib 的 3d View 中绘制 2d 流线

python - 如何在链接数据结构上编写广度优先搜索算法?

python - 如何在 Django Rest Framework 测试中强制进行身份验证

python - 使用Python漫步HDFS