编写一个学生类student,包含的属性有学号、姓名、性别、年龄等,将所有学生存储在一个数组中,编写学生管理程序实现如下操作:(1)增加一个学生;(2)根据学号删除某个学生;(3)将所有学生年龄增加一岁;(4)按数组中顺序显示所有学生信息;(5)将所有学生按姓名排序输出。
时间: 2023-05-29 17:04:38 浏览: 146
class Student:
def __init__(self, number, name, gender, age):
self.number = number
self.name = name
self.gender = gender
self.age = age
def __str__(self):
return f'{self.number}, {self.name}, {self.gender}, {self.age}'
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, number):
for student in self.students:
if student.number == number:
self.students.remove(student)
def increase_age(self):
for student in self.students:
student.age += 1
def show_all_students(self):
for student in self.students:
print(student)
def sort_students_by_name(self):
sorted_students = sorted(self.students, key=lambda x: x.name)
for student in sorted_students:
print(student)
#测试
manager = StudentManager()
student1 = Student('001', 'Tom', 'male', 20)
student2 = Student('002', 'Lucy', 'female', 19)
student3 = Student('003', 'Jack', 'male', 21)
manager.add_student(student1)
manager.add_student(student2)
manager.add_student(student3)
manager.show_all_students()
manager.remove_student('002')
manager.increase_age()
manager.show_all_students()
manager.sort_students_by_name()
阅读全文