有5个学生,每个学生的数据包括学号(num) ,姓名(name) ,总成绩(score) ,编程实现从
时间: 2024-06-12 20:08:17 浏览: 55
这5个学生数据中,找出总成绩最高的学生。
可以使用Python的字典和循环来实现。
首先,定义5个学生的数据,可以使用字典来表示:
students = [
{'num': '101', 'name': 'Tom', 'score': 85},
{'num': '102', 'name': 'Jerry', 'score': 92},
{'num': '103', 'name': 'Peter', 'score': 78},
{'num': '104', 'name': 'Mary', 'score': 88},
{'num': '105', 'name': 'John', 'score': 96}
]
接着,使用循环遍历每个学生的数据,找出总成绩最高的学生:
max_score = 0
max_student = {}
for student in students:
if student['score'] > max_score:
max_score = student['score']
max_student = student
最后,输出总成绩最高的学生的姓名和总成绩:
print('总成绩最高的学生是:', max_student['name'])
print('总成绩为:', max_student['score'])
完整代码如下:
students = [
{'num': '101', 'name': 'Tom', 'score': 85},
{'num': '102', 'name': 'Jerry', 'score': 92},
{'num': '103', 'name': 'Peter', 'score': 78},
{'num': '104', 'name': 'Mary', 'score': 88},
{'num': '105', 'name': 'John', 'score': 96}
]
max_score = 0
max_student = {}
for student in students:
if student['score'] > max_score:
max_score = student['score']
max_student = student
print('总成绩最高的学生是:', max_student['name'])
print('总成绩为:', max_student['score'])
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)