给我写一份学生信息管理系统的代码
时间: 2023-06-26 21:09:38 浏览: 37
好的,以下是一个简单的学生信息管理系统的代码示例:
```python
class Student:
def __init__(self, name, id_num, major, grade):
self.name = name
self.id_num = id_num
self.major = major
self.grade = grade
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, name, id_num, major, grade):
student = Student(name, id_num, major, grade)
self.students.append(student)
def remove_student(self, id_num):
for student in self.students:
if student.id_num == id_num:
self.students.remove(student)
return True
return False
def search_student(self, id_num):
for student in self.students:
if student.id_num == id_num:
return student
return None
def print_all_students(self):
for student in self.students:
print("Name: {}, ID: {}, Major: {}, Grade: {}".format(student.name, student.id_num, student.major, student.grade))
# Example usage
sms = StudentManagementSystem()
sms.add_student("John Smith", "123456", "Computer Science", "Junior")
sms.add_student("Jane Doe", "654321", "Mathematics", "Sophomore")
sms.print_all_students()
# Output:
# Name: John Smith, ID: 123456, Major: Computer Science, Grade: Junior
# Name: Jane Doe, ID: 654321, Major: Mathematics, Grade: Sophomore
sms.remove_student("123456")
john = sms.search_student("123456")
if john is None:
print("John Smith not found")
else:
print("Found John Smith: {} - {}".format(john.id_num, john.major))
# Output: John Smith not found
```