用python类写一个学生信息管理系统、输入并存储学生的信息 2、修改学生信息 3、删除学生信息 4、成绩排序 5、查找学生信息
时间: 2024-01-10 13:02:54 浏览: 84
下面是一个简单的学生信息管理系统的Python类,包含输入并存储学生信息、修改学生信息、删除学生信息、成绩排序和查找学生信息的功能:
```python
class Student:
def __init__(self, name, id, score):
self.name = name
self.id = id
self.score = score
def __str__(self):
return f"{self.name}({self.id}): {self.score}"
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, name, id, score):
student = Student(name, id, score)
self.students.append(student)
def modify_student(self, id, name=None, score=None):
for student in self.students:
if student.id == id:
if name is not None:
student.name = name
if score is not None:
student.score = score
return True
return False
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
return True
return False
def sort_students(self):
self.students.sort(key=lambda x: x.score, reverse=True)
def search_student(self, id):
for student in self.students:
if student.id == id:
return student
return None
def print_students(self):
for student in self.students:
print(student)
```
使用方法如下:
```python
# 创建一个学生管理器对象
manager = StudentManager()
# 添加学生
manager.add_student("张三", "1001", 80)
manager.add_student("李四", "1002", 90)
manager.add_student("王五", "1003", 70)
# 修改学生信息
manager.modify_student("1001", name="张三丰", score=85)
# 删除学生信息
manager.delete_student("1003")
# 成绩排序
manager.sort_students()
# 查找学生信息
student = manager.search_student("1002")
if student is not None:
print(student)
# 打印所有学生信息
manager.print_students()
```
注意:这只是一个简单的示例,实际的学生管理系统可能需要更复杂的功能和数据存储方式。
阅读全文