学生成绩管理系统python
时间: 2023-10-12 14:19:50 浏览: 38
学生成绩管理系统是一个常见的项目,下面是一个简单的实现:
```python
class Student:
def __init__(self, name, id, scores):
self.name = name
self.id = id
self.scores = scores
class GradeBook:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def get_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def get_students_by_name(self, name):
return [student for student in self.students if student.name == name]
def get_average_score_for_student(self, id):
student = self.get_student_by_id(id)
if not student:
return None
return sum(student.scores) / len(student.scores)
def get_average_score_for_all_students(self):
scores = []
for student in self.students:
scores.extend(student.scores)
return sum(scores) / len(scores)
```
这个系统包括两个类:`Student` 和 `GradeBook`。
`Student` 类表示一个学生,包含学生的姓名、学号和成绩列表。
`GradeBook` 类表示一个班级的成绩册,包含学生列表。它包括一些方法:
- `add_student`:添加一个学生到成绩册中。
- `get_student_by_id`:根据学号查找学生。
- `get_students_by_name`:根据姓名查找所有学生。
- `get_average_score_for_student`:计算一个学生的平均成绩。
- `get_average_score_for_all_students`:计算所有学生的平均成绩。
要使用这个系统,你可以创建一个 `GradeBook` 对象,然后添加学生。例如:
```python
gb = GradeBook()
gb.add_student(Student("张三", "001", [80, 90, 85]))
gb.add_student(Student("李四", "002", [70, 85, 90]))
gb.add_student(Student("王五", "003", [75, 80, 75]))
```
然后你就可以使用 `get_student_by_id`、`get_students_by_name`、`get_average_score_for_student` 和 `get_average_score_for_all_students` 方法来查询成绩了。例如:
```python
# 查询学号为 "002" 的学生的平均成绩
print(gb.get_average_score_for_student("002"))
# 查询所有学生的平均成绩
print(gb.get_average_score_for_all_students())
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)