Editor

Fix the Student Class

Intermediate · Fix the bug · from Python Classes and Objects: OOP from the Ground Up

From the lesson: Python Classes and Objects: OOP from the Ground Up

Imagine you own a bakery and you have a cookie cutter shaped like a star. Every time you press it into dough, you get a new star-shaped cookie. The cutter itself isn't a cookie - it's a blueprint for making cookies.

Read the lesson

This Student class has two bugs. Fix them so the code prints:

Alice: 85.0
class Student:
def __init__(self, name):
self.name = name
grades = []
def add_grade(self, grade):
self.grades.append(grade)
def average(self):
return sum(self.grades) / len(self.grades)
s = Student('Alice')
s.add_grade(90)
s.add_grade(80)
print(f'{s.name}: {s.average()}')

More Classes and Objects exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz