使用python设计一个简单的学生管理系统代码
时间: 2024-02-04 07:02:26 浏览: 63
好的,下面是一个简单的学生管理系统的Python代码示例:
```python
class Student:
def __init__(self, name, age, sex, id):
self.name = name
self.age = age
self.sex = sex
self.id = id
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def search_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def show_all_students(self):
for student in self.students:
print("Name: {}, Age: {}, Sex: {}, ID: {}".format(student.name, student.age, student.sex, student.id))
if __name__ == '__main__':
manager = StudentManager()
student1 = Student("Tom", 18, "Male", "001")
student2 = Student("Jerry", 19, "Female", "002")
manager.add_student(student1)
manager.add_student(student2)
manager.show_all_students()
manager.remove_student("001")
manager.show_all_students()
student = manager.search_student("002")
if student:
print("Name: {}, Age: {}, Sex: {}, ID: {}".format(student.name, student.age, student.sex, student.id))
else:
print("Student not found.")
```
以上代码实现了一个简单的学生管理系统,包含了添加学生、删除学生、查找学生、显示所有学生等功能。
阅读全文