生成一个学生信息管理系统代码
时间: 2023-12-25 17:51:28 浏览: 20
好的,以下是一个简单的学生信息管理系统的代码,供您参考:
```python
class Student:
def __init__(self, name, id, major):
self.name = name
self.id = id
self.major = major
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student_by_id(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
def search_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
manager = StudentManager()
s1 = Student("Tom", "123456789", "Computer Science")
s2 = Student("Alice", "987654321", "Electrical Engineering")
s3 = Student("Bob", "345678912", "Mechanical Engineering")
manager.add_student(s1)
manager.add_student(s2)
manager.add_student(s3)
print("All students:")
for student in manager.students:
print(student.name)
manager.remove_student_by_id("123456789")
print("After removing Tom:")
for student in manager.students:
print(student.name)
s4 = manager.search_by_id("987654321")
print("Search result:")
if s4:
print("Name:", s4.name)
print("Major:", s4.major)
else:
print("Not found.")
```
希望这能帮到您,有其他问题欢迎随时提问。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)