python学生管理系统代码

python
class Student: def __init__(self, name, age, grade): self.name = name self.age = age self.grade = grade def display_student(self): print(f"Name: {self.name}, Age: {self.age}, Grade: {self.grade}") class StudentManagementSystem: def __init__(self): self.students = [] def add_student(self, student): self.students.append(student) def remove_student(self, student): if student in self.students: self.students.remove(student) print(f"{student.name} removed from the system.") else: print(f"{student.name} is not in the system.") def display_all_students(self): print("List of all students:") for student in self.students: student.display_student() # 示例用法 if __name__ == "__main__": system = StudentManagementSystem() # 添加学生 student1 = Student("Alice", 20, "A") student2 = Student("Bob", 21, "B") student3 = Student("Charlie", 19, "C") system.add_student(student1) system.add_student(student2) system.add_student(student3) # 显示所有学生 system.display_all_students() # 移除学生 system.remove_student(student2) # 再次显示所有学生 system.display_all_students()

这个代码包含了两个类:StudentStudentManagementSystemStudent 类用于创建学生对象,包括学生的姓名、年龄和成绩。StudentManagementSystem 类用于管理学生对象,包括添加学生、移除学生和显示所有学生信息的功能。

python
class Student: def __init__(self, name, age, grade): self.name = name self.age = age self.grade = grade def display_student(self): print(f"Name: {self.name}, Age: {self.age}, Grade: {self.grade}") class StudentManagementSystem: def __init__(self): self.students = [] def add_student(self, student): self.students.append(student) def remove_student(self, student): if student in self.students: self.students.remove(student) print(f"{student.name} removed from the system.") else: print(f"{student.name} is not in the system.") def display_all_students(self): print("List of all students:") for student in self.students: student.display_student() def search_student(self, name): found = False for student in self.students: if student.name.lower() == name.lower(): print("Student Found:") student.display_student() found = True break if not found: print(f"Student with name '{name}' not found.") def update_student_grade(self, name, new_grade): for student in self.students: if student.name.lower() == name.lower(): student.grade = new_grade print(f"Grade updated for {student.name} to {new_grade}.") break else: print(f"Student with name '{name}' not found.") # 示例用法 if __name__ == "__main__": system = StudentManagementSystem() # 添加学生 student1 = Student("Alice", 20, "A") student2 = Student("Bob", 21, "B") student3 = Student("Charlie", 19, "C") system.add_student(student1) system.add_student(student2) system.add_student(student3) # 显示所有学生 system.display_all_students() # 搜索学生 system.search_student("Bob") # 更新学生成绩 system.update_student_grade("Bob", "A") # 显示所有学生 system.display_all_students()

这个扩展后的学生管理系统添加了搜索学生和更新学生成绩的功能。你可以根据需要进一步扩展该系统,比如持久化存储学生信息、增加更多的属性和方法等。