7-7 找出总分最高的学生python
时间: 2023-08-25 09:10:25 浏览: 51
假设学生信息已经存储在一个字典列表中,每个字典包含学生姓名和各科成绩。可以使用以下代码找出总分最高的学生:
```python
students = [
{'name': 'Alice', 'math': 90, 'english': 80, 'history': 85},
{'name': 'Bob', 'math': 75, 'english': 85, 'history': 95},
{'name': 'Charlie', 'math': 80, 'english': 70, 'history': 90},
]
# 计算每个学生的总分并添加到字典中
for student in students:
student['total'] = student['math'] + student['english'] + student['history']
# 找到总分最高的学生
highest_score = 0
highest_student = None
for student in students:
if student['total'] > highest_score:
highest_score = student['total']
highest_student = student
# 输出结果
print(f"{highest_student['name']} has the highest score of {highest_score}")
```
输出结果:
```
Bob has the highest score of 255
```