编写学生信息管理系统
时间: 2023-07-09 19:18:03 浏览: 47
好的,以下是一个简单的学生信息管理系统的代码示例:
```python
class Student:
def __init__(self, name, id, major):
self.name = name
self.id = id
self.major = major
def __str__(self):
return f"ID: {self.id}\nName: {self.name}\nMajor: {self.major}\n"
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
print("Student added successfully!")
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
print("Student removed successfully!")
return
print("Student not found.")
def find_student_by_id(self, id):
for student in self.students:
if student.id == id:
print(student)
return
print("Student not found.")
def list_students(self):
if len(self.students) == 0:
print("No students found.")
else:
for student in self.students:
print(student)
# Example usage
sms = StudentManagementSystem()
# Add students
s1 = Student("John Smith", 1001, "Computer Science")
s2 = Student("Jane Doe", 1002, "Electrical Engineering")
sms.add_student(s1)
sms.add_student(s2)
# List students
sms.list_students()
# Find student by ID
sms.find_student_by_id(1001)
# Remove student
sms.remove_student(1002)
# List students again
sms.list_students()
```
这个系统包含了一个 `Student` 类和一个 `StudentManagementSystem` 类。`Student` 类用于表示一个学生的信息,包括姓名、学号和专业。`StudentManagementSystem` 类用于管理学生信息,包括添加、删除、查找和列出学生。
在示例中,我们先创建一个 `StudentManagementSystem` 实例,然后添加两个学生,列出学生信息,查找一个学生,删除一个学生,最后再列出一次学生信息。
你可以根据自己的需求修改和扩展这个系统。