python class Student: def __init__(self, name, score): self.name = name self.score = score class StudentIterator: def __init__(self, students): self.students = students self.current = 0 def __next__(self): if self.current >= len(self.students): raise StopIteration else: student = self.students[self.current] self.current += 1 return student def __iter__(self): return self class StudentCollection: def __init__(self): self.students = [] def add_student(self, student): self.students.append(student) def __iter__(self): return StudentIterator(self.students) def calculate_average_score(student_collection): total_score = 0 count = 0 for student in student_collection: total_score += student.score count += 1 return total_score / count collection = StudentCollection() average_score = calculate_average_score(collection)


上一篇:
下一篇:
切换中文