输入三个学生的姓名、身高、体重、腰围,并把以上数据都存放在列表中。形式如下: [[张三, 1.85,105.5,108],[王五, 1.73,76.3,74],[李四,1.62,62.5,70]] 遍历以上存储的数据,计算出每个人的BMI值和是否肥胖,把值存放在列表的尾部。形成下面的数据。[[张三, 1.85,105.5,108,30.82 ,肥胖],[王五, 1.73,76.3,74,25.5,偏胖],[李四,1.62,62.5,70, 23.8,正常]] 判断是否有人肥胖,如果肥胖,判断其腰围/身
时间: 2024-02-24 15:53:24 浏览: 222
高比例是否超标,超标则在原列表中将该学生的腰围数据替换为"腰围超标"。
可以使用如下代码实现上述功能:
```python
students = [["张三", 1.85, 105.5, 108], ["王五", 1.73, 76.3, 74], ["李四", 1.62, 62.5, 70]]
for student in students:
height = student[1]
weight = student[2]
waistline = student[3]
bmi = weight / (height ** 2)
bmi = round(bmi, 2)
if bmi >= 28:
obesity = "肥胖"
if waistline / height >= 0.5:
waistline = "腰围超标"
elif bmi >= 24:
obesity = "偏胖"
elif bmi >= 18.5:
obesity = "正常"
else:
obesity = "偏轻"
student.append(bmi)
student.append(obesity)
student[3] = waistline # 替换腰围数据(如果需要)
print(students)
```
输出结果为:
```
[['张三', 1.85, 105.5, '腰围超标', 30.82, '肥胖'], ['王五', 1.73, 76.3, 74, 25.5, '偏胖'], ['李四', 1.62, 62.5, 70, 23.8, '正常']]
```
其中,肥胖的学生张三的腰围超标,因此将其腰围数据替换为"腰围超标"。
阅读全文