用python设计一个学生管理系统
时间: 2023-09-23 07:03:58 浏览: 95
好的,我可以为您提供一个 Python 实现的学生管理系统的示例代码,如下所示:
```python
import pickle
class Student:
def __init__(self, name, id, gender, age, grade):
self.name = name
self.id = id
self.gender = gender
self.age = age
self.grade = grade
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
def search_student_by_name(self, name):
for student in self.students:
if student.name == name:
return student
return None
def search_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def display_all_students(self):
for student in self.students:
print("Name: {}, ID: {}, Gender: {}, Age: {}, Grade: {}".format(student.name, student.id, student.gender, student.age, student.grade))
def save_to_file(self, filename):
with open(filename, 'wb') as f:
pickle.dump(self.students, f)
def load_from_file(self, filename):
with open(filename, 'rb') as f:
self.students = pickle.load(f)
def main():
s1 = Student("Tom", "101", "Male", 18, 80)
s2 = Student("Jerry", "102", "Male", 19, 90)
s3 = Student("Lucy", "103", "Female", 20, 85)
sms = StudentManagementSystem()
sms.add_student(s1)
sms.add_student(s2)
sms.add_student(s3)
sms.display_all_students()
s = sms.search_student_by_name("Tom")
if s:
print("Found student: Name: {}, ID: {}, Gender: {}, Age: {}, Grade: {}".format(s.name, s.id, s.gender, s.age, s.grade))
sms.save_to_file("students.dat")
sms.remove_student(s2)
sms.load_from_file("students.dat")
sms.display_all_students()
if __name__ == '__main__':
main()
```
这个学生管理系统使用了 Python 的面向对象编程思想,定义了一个 `Student` 类来存储学生的信息,定义了一个 `StudentManagementSystem` 类来管理学生信息,并提供了添加、删除、查询和显示学生信息等方法。此外,该系统还提供了数据存储和读取功能,使用了 Python 标准库中的 `pickle` 模块来实现。
阅读全文