编写程序,创建一个数列,存储10个同学某项技能比赛成绩,计算并输出该项技能比赛前三名成绩及平均成绩python
时间: 2024-10-23 22:04:25 浏览: 21
你可以使用Python编写一个简单的程序来实现这个需求。首先,你需要创建一个列表来存储10个同学的成绩,并使用元组或字典来保持每个学生的姓名和成绩。然后,可以对这些成绩进行排序,找出前三个最高分以及所有成绩的平均值。以下是示例代码:
```python
# 创建一个字典列表存储学生及其成绩
students_scores = [
{"name": "张三", "score": 95},
{"name": "李四", "score": 98},
{"name": "王五", "score": 92},
{"name": "赵六", "score": 97},
{"name": "陈七", "score": 96},
{"name": "刘八", "score": 94},
{"name": "杨九", "score": 93},
{"name": "吴十", "score": 91},
]
# 对成绩列表按降序排列
sorted_students = sorted(students_scores, key=lambda x: x["score"], reverse=True)
# 计算前三名成绩
top_three_scores = [student["score"] for student in sorted_students[:3]]
# 计算平均成绩
average_score = sum(student["score"] for student in students_scores) / len(students_scores)
# 输出结果
print("前三名成绩:")
for i, score in enumerate(top_three_scores):
print(f"第{i+1}名:{sorted_students[i]['name']} - {score}")
print(f"\n平均成绩:{average_score}")
```
阅读全文