已知有一个包含一些同学成绩的字典:scores = {"Zhang San": 45, "Li Si": 78, "Wang Wu": 40, "Zhou Liu": 96,"Zhao Qi": 65, "Sun Ba": 90, "Zheng Jiu": 78, "Wu Shi": 99,"Dong Shiyi": 60},编写程序完成如下功能: (1)请循环遍历出所有的 value (2)请循环遍历出所有的 key 和 value (3)输出成绩的最高分、最低分和平均分 (4)输出不及格的同学名字和分数
时间: 2023-07-02 12:15:28 浏览: 236
下面是代码实现:
```python
# 定义字典
scores = {"Zhang San": 45, "Li Si": 78, "Wang Wu": 40, "Zhou Liu": 96,
"Zhao Qi": 65, "Sun Ba": 90, "Zheng Jiu": 78, "Wu Shi": 99, "Dong Shiyi": 60}
# 遍历所有的 value
for score in scores.values():
print(score)
# 遍历所有的 key 和 value
for name, score in scores.items():
print(name, score)
# 计算最高分、最低分和平均分
score_list = list(scores.values())
max_score = max(score_list)
min_score = min(score_list)
avg_score = sum(score_list) / len(score_list)
print("最高分:", max_score)
print("最低分:", min_score)
print("平均分:", avg_score)
# 输出不及格同学名字和分数
for name, score in scores.items():
if score < 60:
print(name, score)
```
输出结果如下:
```
45
78
40
96
65
90
78
99
60
Zhang San 45
Li Si 78
Wang Wu 40
Zhou Liu 96
Zhao Qi 65
Sun Ba 90
Zheng Jiu 78
Wu Shi 99
Dong Shiyi 60
最高分: 99
最低分: 40
平均分: 70.11111111111111
Zhang San 45
Wang Wu 40
Dong Shiyi 60
```
阅读全文