好的,这是一份简单的学生管理系统的 Python 代码:
``` class Student: def init(self, name, age, gender, student_id): self.name = name self.age = age self.gender = gender self.student_id = student_id
def get_student_info(self):
return f"姓名: {self.name}, 年龄: {self.age}, 性别: {self.gender}, 学号: {self.student_id}"
class StudentManager: def init(self): self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student_id):
for student in self.students:
if student.student_id == student_id:
self.students.remove(student)
break
def update_student(self, student_id, new_student):
for student in self.students:
if student.student_id == student_id:
student.name = new_student.name
student.age = new_student.age
student.gender = new_student.gender
break
def get_student_by_id(self, student_id):
for student in self.students:
if student.student_id == student_id:
return student
return None
def get_all_students(self):
return self.students
创建学生管理器
manager = StudentManager()
创建学生
student1 = Student("张三", 20, "男", "20220001") student2 = Student("李四", 21, "女", "20220002") student3 = Student("王五", 22, "男", "20220003")
添加学生
manager.add_student(student1) manager.add_student(student2) manager.add_student(student3)
获取所有学生
students = manager.get_all_students() for student in students: print(student.get_student_info())
根据学号查找学生
student = manager.get_student_by_id("20220001") if student: print(f"找到学生: {student.get_student_info()}") else: print("未找到学生")
修改学生信息
new_student = Student("李小六", 23, "女", "20220001") manager.update_student("20220001", new_student) student = manager.get_student_by_id("20220001") if student: print(f"找到学生: {student.get