编写一个python程序,用户输入身高和体重,BMI=
时间: 2023-06-02 14:06:08 浏览: 537
体重(kg) / 身高(m)^2,请编写一个python程序,计算并输出用户的BMI。
好的,我可以回答这个问题,请输入以下代码:
```python
height = float(input("请输入您的身高(米):"))
weight = float(input("请输入您的体重(千克):"))
# 计算BMI指数
bmi = weight / (height ** 2)
print("您的BMI指数为:%.2f" % bmi)
```
请注意,以上代码只是一个简单的样例,具体实现视情况而定。
相关问题
python请编写一个身体质量指数BMI测试的程序,计算公式为BMI=体重(千克)/身高(米)的平方,成人标准值是BMI18.5-23.9,请输入某人的体重和身高给出偏瘦、正常和偏胖的结论
好的,根据题目描述,这里是 Python 代码实现:
```python
weight = float(input("请输入体重(kg):"))
height = float(input("请输入身高(m):"))
# 计算BMI指数
bmi = weight / (height ** 2)
# 判断BMI指数的范围
if bmi < 18.5:
print("偏瘦")
elif bmi <= 23.9:
print("正常")
else:
print("偏胖")
```
解释一下代码:
1. 首先使用 `input` 函数获取用户输入的体重和身高,并使用 `float` 函数将其转换为浮点数类型,分别保存到 `weight` 和 `height` 变量中。
2. 然后使用公式 `bmi = weight / (height ** 2)` 计算BMI指数,保存到 `bmi` 变量中。
3. 使用 `if...elif...else` 结构判断 `bmi` 的范围。
4. 如果 `bmi` 小于 18.5,则输出“偏瘦”。
5. 如果 `bmi` 大于等于 18.5 且小于等于 23.9,则输出“正常”。
6. 如果 `bmi` 大于 23.9,则输出“偏胖”。
希望能帮到你!
请python编写一个函数getBMI,它通过两个参数获得身高和体重,并返回一个由BMI数值和肥胖程度字符串两个元素构成的元组。在主程序中由用户输入身高和体重,通过调用自己编写的这个getBMI函数,打印BMI计算结果
好的,我明白了。以下是您需要的代码:
```
def getBMI(height, weight):
bmi = weight / (height ** 2)
if bmi < 18.5:
obesity = "偏瘦"
elif bmi < 24:
obesity = "正常"
elif bmi < 28:
obesity = "偏胖"
else:
obesity = "肥胖"
return (bmi, obesity)
height = float(input("请输入身高(米):"))
weight = float(input("请输入体重(千克):"))
result = getBMI(height, weight)
print("BMI值为:%.2f,肥胖程度为:%s" % result)
```
请注意,我已经将代码复制到文本框中,您只需将其粘贴到你的Python编辑器中即可。请确保代码的缩进是正确的。
此代码使用户输入身高和体重并计算BMI值。然后,它使用if-elif-else语句确定肥胖程度,并返回一个元组,其中第一个元素是BMI值,第二个元素是肥胖程度字符串。
我希望这可以回答您的问题。如果您还有其他问题,请告诉我。
阅读全文
相关推荐
















