帮我写基于python的学生管理系统
时间: 2023-07-13 13:24:29 浏览: 151
好的,下面是一个简单的学生管理系统的Python代码示例:
```python
class Student:
def __init__(self, name, id, grade):
self.name = name
self.id = id
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def get_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def get_student_by_name(self, name):
result = []
for student in self.students:
if student.name == name:
result.append(student)
return result
def get_all_students(self):
return self.students
if __name__ == '__main__':
manager = StudentManager()
manager.add_student(Student('Tom', '001', 90))
manager.add_student(Student('Jerry', '002', 80))
print(manager.get_all_students())
student = manager.get_student_by_id('001')
if student:
print(student.name, student.grade)
manager.remove_student('002')
print(manager.get_all_students())
students = manager.get_student_by_name('Tom')
for student in students:
print(student.id, student.grade)
```
这个简单的学生管理系统包括两个类:`Student`和`StudentManager`。`Student`类表示一个学生,包括姓名、学号和成绩;`StudentManager`类表示学生管理器,包括添加学生、删除学生、按学号或姓名查找学生以及获取所有学生信息等功能。
在`main`函数中,我们首先创建一个`StudentManager`对象`manager`,然后添加两个学生信息到`manager`中。接着,我们分别通过学号和姓名查找学生信息,并输出结果。然后,我们删除学号为`002`的学生,并输出剩余的所有学生信息。最后,我们再次按姓名查找学生信息,输出所有名为`Tom`的学生的学号和成绩。
这只是一个简单的示例,实际的学生管理系统可能需要更多的功能和更复杂的实现。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)