请用python1、输入并存储学生的信息 2、修改学生信息 3、删除学生信息 4、成绩排序 5、查找学生信息 6、进行测试 写出一个完整的代码
时间: 2023-12-27 12:03:02 浏览: 44
好的,以下是一个简单的实现:
```python
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def modify_student(self, name, age, gender, grade):
for student in self.students:
if student.name == name:
student.age = age
student.gender = gender
student.grade = grade
break
def delete_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
break
def sort_students(self):
self.students = sorted(self.students, key=lambda student: student.grade, reverse=True)
def find_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
def test(self):
student1 = Student("Alice", 18, "female", 90)
student2 = Student("Bob", 19, "male", 85)
student3 = Student("Charlie", 20, "male", 95)
self.add_student(student1)
self.add_student(student2)
self.add_student(student3)
self.modify_student("Alice", 19, "female", 92)
self.delete_student("Bob")
self.sort_students()
print("Sorted students:")
for student in self.students:
print("Name: {}, Age: {}, Gender: {}, Grade: {}".format(student.name, student.age, student.gender, student.grade))
print("Found student:")
student = self.find_student("Charlie")
print("Name: {}, Age: {}, Gender: {}, Grade: {}".format(student.name, student.age, student.gender, student.grade))
if __name__ == "__main__":
manager = StudentManager()
manager.test()
```
这个代码实现了一个简单的学生信息管理系统,可以添加、修改、删除、排序和查找学生信息,并且包含了一个简单的测试函数。
阅读全文