用python写学生信息管理系统
时间: 2023-09-30 16:06:28 浏览: 15
好的,下面是一个简单的学生信息管理系统的示例代码:
```python
class Student:
def __init__(self, id, name, age, gender):
self.id = id
self.name = name
self.age = age
self.gender = gender
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, id, name, age, gender):
student = Student(id, name, age, gender)
self.students.append(student)
def remove_student_by_id(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def search_student_by_id(self, id):
for student in self.students:
if student.id == id:
return student
return None
def search_student_by_name(self, name):
results = []
for student in self.students:
if student.name == name:
results.append(student)
return results
def get_all_students(self):
return self.students
# 测试代码
manager = StudentManager()
manager.add_student("1001", "Tom", 20, "male")
manager.add_student("1002", "Jerry", 22, "male")
manager.add_student("1003", "Lucy", 21, "female")
print("All students:")
for student in manager.get_all_students():
print(student.id, student.name, student.age, student.gender)
print("Search student by id:")
student = manager.search_student_by_id("1002")
if student:
print(student.id, student.name, student.age, student.gender)
else:
print("Student not found")
print("Search student by name:")
students = manager.search_student_by_name("Tom")
if students:
for student in students:
print(student.id, student.name, student.age, student.gender)
else:
print("Student not found")
print("Remove student by id:")
if manager.remove_student_by_id("1003"):
print("Student removed")
else:
print("Student not found")
```
该示例代码中,我们定义了一个 `Student` 类,用于表示学生信息,包括学号、姓名、年龄和性别。然后,我们定义了一个 `StudentManager` 类,用于管理学生信息,包括添加、删除、查找等操作。
在测试代码中,我们首先创建了一个 `StudentManager` 对象,并添加了三个学生信息。然后,我们分别测试了按学号查找学生、按姓名查找学生、删除学生等操作,并输出了相应的结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)