Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python错误结果中的BMI计算_Python_Python 3.x_Calculator - Fatal编程技术网

Python错误结果中的BMI计算

Python错误结果中的BMI计算,python,python-3.x,calculator,Python,Python 3.x,Calculator,我必须为列表中的人创建BMI计算 patients = [[70, 1.8], [80, 1.9], [150, 1.7]] 现在我的功能是: def calculate_bmi(weight, height): return weight / (height ** 2) for patient in patients: weight, height = patients[0] bmi = round(weight/ (height * height), 1)

我必须为列表中的人创建BMI计算

patients = [[70, 1.8], [80, 1.9], [150, 1.7]]
现在我的功能是:

def calculate_bmi(weight, height):
    return weight / (height ** 2)

for patient in patients:
    weight, height = patients[0]
    bmi = round(weight/ (height * height), 1)
    print("Patient's BMI is: %f" % bmi)
她给我看了三次第一人称的结果[0]。我知道我在函数中有第一人称值。 第二件事是如何将BMI值减少到小数点后的一位数,例如21.6

Patient's BMI is: 21.600000
Patient's BMI is: 21.600000
Patient's BMI is: 21.600000
我不知道如何保存一个函数来计算列表中每个人的BMI。
有人能帮我吗?Thx获取答案。

您使用
体重,身高=患者[0]
反复将体重和身高设置为患者列表中的第一个元素;请记住,for循环正在迭代列表中的所有患者,因此您可以只使用patient变量,如下所示

def calculate_bmi(weight, height):
    return weight / (height ** 2.0)


for patient in patients:
    weight, height = patient
    bmi = round(weight/ (height * height), 1)
    print("Patient's BMI is: %f" % bmi)

您需要更改代码中的一些内容。
当您在
患者
列表上迭代时,需要分配
体重,身高=患者[0],患者[1]

只需检查以下代码:

for patient in patients:
     weight, height = patient[0],patient[1]
     bmi = round(weight/ (height * height), 1)
     print("Patient's BMI is: ", bmi)

另外,从
打印功能中删除
%f
,只在小数点后显示一位。

体重、身高=患者
,而不是
患者[0]