Create game using classes and objects

Hello,

I’m having some problems with my code, and keep getting an error message:

class Maths:
def init(self, name, teacher):
self.name = name
self.teacher = teacher
self.students =

def add_student(self, student_name):
self.students.append(student_name)

def get_student_count(self):
return len(self.students)

def repr(self):
return f"Maths Class: {self.name}, Teacher: {self.teacher}, Students: {self.students}"

class Science:
def init(self, name, teacher):
self.name = name
self.teacher = teacher
self.students =

def add_student(self, student_name):
self.students.append(student_name)

def get_student_count(self):
return len(self.students)

def repr(self):
return f"Science Class: {self.name}, Teacher: {self.teacher}, Students: {self.students}"

maths_class1 = Maths(“Algebra”, “Mr. Thomas”)
maths_class1.add_student(“James”)
maths_class1.add_student(“Sharon”)

maths_class2 = Maths(“Quadratics”, “Ms. Peterson”)
maths_class2.add_student(“Michael”)
maths_class2.add_student(“Denise”)

science_class1 = Science(“Biology”, “Mrs. Smith”)
science_class1.add_student(“David”)
science_class1.add_student(“Emily”)

science_class2 = Science(“Physics”, “Mr. Davies”)
science_class2.add_student(“Frank”)
science_class2.add_student(“Grace”)

maths_classes = [maths_class1, maths_class2]
science_classes = [science_class1, science_class2]

for maths_class in maths_classes:
print(maths_class.get_class_details())
print(“---------------”)

for science_class in science_classes:
print(science_class.get_class_details())
print(“---------------”)

The error I’m getting is:

SyntaxError: invalid character in identifier
$ python3 script.py
Traceback (most recent call last):
File “script.py”, line 51, in
print(maths_class.get_class_details())
AttributeError: ‘Maths’ object has no attribute ‘get_class_details’

I’m currently in here working - https://www.codecademy.com/journeys/computer-science/paths/cscj-22-intro-to-programming/tracks/cscj-22-basic-python-data-structures-and-objects/modules/cscj-22-python-object-oriented-programming/projects/create-a-game-using-classes-and-objects

Any assistance would be greatly appreciated

There does not seem to be a get_class_details method (function) for the Maths class.
There is a __repr__ method for the Maths class,
so you may be able to do
print(maths_class)
instead of
print(maths_class.get_class_details())

Next time, please use the </> button and post your code on lines between the ``` and ``` to keep the formatting of the code (like spacing and indents)

2 Likes