利用类和对象进行成绩处理
时间: 2023-05-27 18:08:08 浏览: 57
以下是一个简单的成绩处理程序:
```python
class Student:
def __init__(self, name, score):
self.name = name
self.score = score
def print_info(self):
print(f"Name: {self.name}, Score: {self.score}")
class ScoreProcessor:
def __init__(self, students):
self.students = students
def get_total_score(self):
total_score = 0
for student in self.students:
total_score += student.score
return total_score
def get_average_score(self):
total_score = self.get_total_score()
return total_score / len(self.students)
def get_highest_score(self):
highest_score = 0
for student in self.students:
if student.score > highest_score:
highest_score = student.score
return highest_score
def get_lowest_score(self):
lowest_score = 100
for student in self.students:
if student.score < lowest_score:
lowest_score = student.score
return lowest_score
if __name__ == "__main__":
students = [
Student("Tom", 80),
Student("Jerry", 90),
Student("Alice", 70),
Student("Bob", 85)
]
processor = ScoreProcessor(students)
print(f"Total score: {processor.get_total_score()}")
print(f"Average score: {processor.get_average_score()}")
print(f"Highest score: {processor.get_highest_score()}")
print(f"Lowest score: {processor.get_lowest_score()}")
for student in students:
student.print_info()
```
输出结果:
```
Total score: 325
Average score: 81.25
Highest score: 90
Lowest score: 70
Name: Tom, Score: 80
Name: Jerry, Score: 90
Name: Alice, Score: 70
Name: Bob, Score: 85
```