用Python求bmi
时间: 2023-11-19 11:51:58 浏览: 36
好的,以下是用 Python 求 BMI 的代码:
```python
height = float(input("请输入身高(米):"))
weight = float(input("请输入体重(千克):"))
bmi = weight / (height ** 2)
print("您的BMI指数为:{:.2f}".format(bmi))
```
其中,`input()` 函数用于接收用户输入的身高和体重,`float()` 函数用于将输入的字符串转换为浮点数,`**` 运算符表示乘方运算。
相关问题
python求bmi值
在Python中计算BMI(Body Mass Index,身体质量指数)可以通过以下步骤实现:
1. 首先,需要获取用户的身高和体重输入。可以使用`input()`函数来获取用户的输入,例如:
```python
height = float(input("请输入您的身高(单位:米):"))
weight = float(input("请输入您的体重(单位:千克):"))
```
2. 接下来,使用以下公式计算BMI值:
```python
bmi = weight / (height ** 2)
```
3. 最后,打印出计算得到的BMI值:
```python
print("您的BMI值为:", bmi)
```
请注意,这只是一个简单的示例,实际应用中可能还需要进行一些错误处理和数据验证。
python求bmi求三次
好的,以下是 Python 代码实现:
```python
def calculate_bmi(weight, height):
'''计算BMI指数'''
bmi = weight / (height ** 2)
return bmi
# 第一次计算BMI
weight1 = 60
height1 = 1.7
bmi1 = calculate_bmi(weight1, height1)
print("第一次计算的BMI指数为:%.2f" % bmi1)
# 第二次计算BMI
weight2 = 70
height2 = 1.75
bmi2 = calculate_bmi(weight2, height2)
print("第二次计算的BMI指数为:%.2f" % bmi2)
# 第三次计算BMI
weight3 = 80
height3 = 1.8
bmi3 = calculate_bmi(weight3, height3)
print("第三次计算的BMI指数为:%.2f" % bmi3)
```
这段代码定义了一个 `calculate_bmi` 函数用于计算 BMI 指数,然后分别给出了三组体重和身高数据进行 BMI 指数的计算,并输出结果。