以下是一个示例解决方法,包含代码示例:
假设有一个班级的学生信息,包括姓名和成绩。我们希望从中筛选出成绩优异的学生并进行展示。
Student
,包含姓名和成绩属性:class Student:
def __init__(self, name, score):
self.name = name
self.score = score
Classroom
,包含学生列表和相关方法:class Classroom:
def __init__(self, students):
self.students = students
def get_excellent_students(self):
excellent_students = []
for student in self.students:
if student.score >= 90:
excellent_students.append(student)
return excellent_students
student1 = Student("张三", 95)
student2 = Student("李四", 85)
student3 = Student("王五", 92)
classroom = Classroom([student1, student2, student3])
get_excellent_students
方法获取成绩优异的学生,并展示结果:excellent_students = classroom.get_excellent_students()
print("成绩优异的学生:")
for student in excellent_students:
print("姓名:{},成绩:{}".format(student.name, student.score))
运行以上代码,输出结果如下:
成绩优异的学生:
姓名:张三,成绩:95
姓名:王五,成绩:92
通过以上代码示例,我们实现了根据成绩筛选出优异学生的功能,并展示了结果。你可以根据自己的实际需求进行相应的修改和扩展。