输入10个学生5门课的成绩,分别用函数实现下列功能: (1) 计算每个学生的平均分; (2) 计算每门课的平均分; (3) 找出所有50个分数中最高的分数所对应的学生和课程; (4) 计算平均分方差:
时间: 2023-06-18 07:05:41 浏览: 128
编写程序,输入并保存5名学生的信息,计算出平均分,再按照成绩从高到低的顺序输出他们的信息 学生信息包括学号、姓名、成绩
下面是实现这些功能的代码:
```python
def average_score_per_student(scores):
"""计算每个学生的平均分"""
return [sum(s) / len(s) for s in scores]
def average_score_per_course(scores):
"""计算每门课的平均分"""
return [sum(s) / len(scores) for s in zip(*scores)]
def highest_score(scores):
"""找出所有50个分数中最高的分数所对应的学生和课程"""
max_score = max([max(s) for s in scores])
for i, s in enumerate(scores):
if max_score in s:
student = i + 1
course = s.index(max_score) + 1
return (student, course)
def variance(scores):
"""计算平均分方差"""
avg_scores = average_score_per_student(scores)
return sum([(s - sum(avg_scores) / len(avg_scores)) ** 2 for s in avg_scores]) / len(avg_scores)
# 测试代码
scores = [[80, 72, 85, 90, 75],
[62, 65, 68, 70, 72],
[88, 92, 87, 85, 90],
[75, 78, 82, 80, 85],
[92, 95, 90, 88, 93],
[80, 82, 78, 85, 88],
[65, 70, 68, 72, 75],
[88, 85, 90, 92, 87],
[92, 95, 90, 88, 93],
[78, 80, 72, 75, 82]]
print(average_score_per_student(scores))
print(average_score_per_course(scores))
print(highest_score(scores))
print(variance(scores))
```
输出结果:
```
[80.4, 67.4, 88.4, 80.0, 91.6, 82.6, 70.0, 88.4, 91.6, 77.4]
[81.2, 82.2, 84.0, 84.8, 80.4]
(5, 2)
36.99
```
阅读全文