编写一个学生类student包含的属性有学号姓名性别年龄等将所有学生存储在一个数组中编写学生管理程序实现如下操作一增加一个学生二根据学号删除某个学生三将所有学生年龄增加一岁4a数组中顺序显示所有学生信息五将所有学生按姓名排序输出
时间: 2023-05-28 13:07:30 浏览: 154
class Student:
def __init__(self, id, name, gender, age):
self.id = id
self.name = name
self.gender = gender
self.age = age
def __str__(self):
return f"{self.id} {self.name} {self.gender} {self.age}"
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def delete_student(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
def increase_age(self):
for student in self.students:
student.age += 1
def display_students(self):
for student in self.students:
print(student)
def sort_by_name(self):
self.students = sorted(self.students, key=lambda student: student.name)
# example usage
manager = StudentManager()
# add students
manager.add_student(Student(1, "Alice", "F", 20))
manager.add_student(Student(2, "Bob", "M", 21))
manager.add_student(Student(3, "Charlie", "M", 19))
# display all students
manager.display_students()
# delete student by id
manager.delete_student(2)
# display all students
manager.display_students()
# increase age of all students
manager.increase_age()
# display all students
manager.display_students()
# sort students by name and display
manager.sort_by_name()
manager.display_students()
阅读全文