如何解决需要帮助遍历嵌套列表,但
'''
patients = [[175.8,73.4],[180.2,59.5],[165.4,70.2],[193.5,120]]
def calculate_bmi(height,weight):
return weight / ((height / 100 )**2)
def get_bmi_category(bmi):
if bmi < 18.5:
return "underweight"
elif bmi < 25.0:
return "normal weight"
elif bmi < 30:
return "overweighting"
else:
return "obesity"
for patient in patients:
height,weight = patients[0]
bmi = calculate_bmi(height,weight)
bmi_category = get_bmi_category(bmi)
print("Patient's BMI is: {} ({})".format(bmi,bmi_category))
'''
打印时,当我想要所有嵌套循环的结果时,我只会得到第一个嵌套列表的结果四次。我还能做什么?
解决方法
将height,weight = patients[0]
更改为height,weight = patient
。
问题出在下面的代码行
height,weight = patients[0]
上面的只会为身高和体重分配[175.8,73.4]值。 如下更新
height,weight = patient
或
for height,weight in patients:
#in this case you'll have to remove,height,weight = patients[0]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。