用python,实现一个成绩统计类ScoreStats,具有以下属性和方法:属性:scores:成绩列表,其中每个元素为一个学生的三门课成绩,例如【[90,85,92],[78,82,80],[95,89,93]】 方法:get_avg_scores():获取三门课程的平均分数 get_max_scores():获取三门课程的最高分数 get_min_scores():获取三门课程的最低分数 get_pass_rate():获取及格率,即三门课程均不低于60分的学生比例
时间: 2023-12-06 17:42:21 浏览: 37
以下是实现ScoreStats类的代码:
```python
class ScoreStats:
def __init__(self, scores):
self.scores = scores
def get_avg_scores(self):
num_students = len(self.scores)
num_courses = len(self.scores[0])
total_scores = [0] * num_courses
for i in range(num_students):
for j in range(num_courses):
total_scores[j] += self.scores[i][j]
avg_scores = [round(total_scores[i]/num_students,2) for i in range(num_courses)]
return avg_scores
def get_max_scores(self):
num_courses = len(self.scores[0])
max_scores = [max([self.scores[i][j] for i in range(len(self.scores))]) for j in range(num_courses)]
return max_scores
def get_min_scores(self):
num_courses = len(self.scores[0])
min_scores = [min([self.scores[i][j] for i in range(len(self.scores))]) for j in range(num_courses)]
return min_scores
def get_pass_rate(self):
num_students = len(self.scores)
num_passed = 0
for i in range(num_students):
if min(self.scores[i]) >= 60:
num_passed += 1
pass_rate = round(num_passed/num_students,2)
return pass_rate
```
我们可以通过以下代码进行测试:
```python
scores = [[90,85,92],[78,82,80],[95,89,93]]
score_stats = ScoreStats(scores)
print("平均分数: ", score_stats.get_avg_scores())
print("最高分数: ", score_stats.get_max_scores())
print("最低分数: ", score_stats.get_min_scores())
print("及格率: ", score_stats.get_pass_rate())
```
输出结果为:
```
平均分数: [87.67, 85.33, 88.33]
最高分数: [95, 89, 93]
最低分数: [78, 82, 80]
及格率: 1.0
```
这说明我们已经成功地实现了ScoreStats类,并且可以正确地计算出成绩统计信息。
阅读全文