Create a Game Using Classes and Objects

Hi,

I am having some problems with some parts of my code. My plan was to create two classe, History and English. The two classes shound interact with each other. This is the code:

class History:
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"History Class: {self.name}, Teacher: {self.teacher}, Students: {self.students}"

class English:
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"English Class: {self.name}, Teacher: {self.teacher}, Students: {self.students}"

history_class1 = History(“World Histoy”, “Mr Johnson”)
history_class1.add_student(“Charlie”)
history_class1.add_student(“Inga”)

history_class2 = History(“European History”, “Mrs. Anderson”)
history_class2.add_student(“Charlie”)
history_class2.add_student(“Daisy”)

english_class1 = English(“English Literature”, “Mrs. Smith”)
english_class1.add_student(“David”)
english_class1.add_student(“Emily”)

english_class2 = English(“English Language”, “Mr. Davis”)
english_class2.add_student(“Frank”)
english_class2.add_student(“Grace”)

history_classes = [history_class1, history_class2]
english_classes = [english_class1, english_class2]

for history_class in history_classes:
print(history_class.get_class_details())
print(“------------------”)

for english_class in english_classes:
print(english_class.get_class_details())
print(“------------------”)

Unfortunately, I get this error message:

Traceback (most recent call last):
File “script.py”, line 51, in
print(history_class.get_class_details())
AttributeError: ‘History’ object has no attribute ‘get_class_details’

Am a little bit stuck here and would be really thankful for some hint.

please format your code so people can read it & help debug it.

2 Likes
class History: 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"History Class: {self.name}, Teacher: {self.teacher}, Students: {self.students}" class English: 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"English Class: {self.name}, Teacher: {self.teacher}, Students: {self.students}" history_class1 = History(“World Histoy”, “Mr Johnson”) history_class1.add_student(“Charlie”) history_class1.add_student(“Inga”) history_class2 = History(“European History”, “Mrs. Anderson”) history_class2.add_student(“Charlie”) history_class2.add_student(“Daisy”) english_class1 = English(“English Literature”, “Mrs. Smith”) english_class1.add_student(“David”) english_class1.add_student(“Emily”) english_class2 = English(“English Language”, “Mr. Davis”) english_class2.add_student(“Frank”) english_class2.add_student(“Grace”) history_classes = [history_class1, history_class2] english_classes = [english_class1, english_class2] for history_class in history_classes: print(history_class.get_class_details()) print(“------------------”) for english_class in english_classes: print(english_class.get_class_details()) print(“------------------”)

Hi ruby6038564967!
I hope you’re doing well!

Some things to consider:

  1. Try to format your code properly, there are several ways to do this. You choose the Codecademy logo, which creates a Codebyte, but you just copy and paste from your original post, but without proper indentation, also, look that self.students in the class doesn’t create an empty list. That will dramatically decrease the chances of getting an answer because most people will not take the time to properly format it. You can create a codebyte, or you can use the Preformatted text option to create the code like this:
Code goes here
  1. Now regarding your code, there are two small issues with your code: I’ll not give you the answer, but some hints:
  • Remember init and repr are special methods to a predefined purpose and are automatically called under certain circumstances. You have a typo when you create them when creating the class.
  • You have to call an attribute you’ve previously defined, and you haven’t defined yet .get_class_details()

With those changes, the output will be:

History Class: World History, Teacher: Mr. Johnson, Students: ['Charlie', 'Inga']
------------------
History Class: European History, Teacher: Mrs. Anderson, Students: ['Charlie', 'Daisy']
------------------
English Class: English Literature, Teacher: Mrs. Smith, Students: ['David', 'Emily']
------------------
English Class: English Language, Teacher: Mr. Davis, Students: ['Frank', 'Grace']
------------------

Let me know if this helps! Also, let me know if you want the answer.
Happy coding.

2 Likes

Thx a lot, I found the mistake! :slight_smile:

2 Likes

please share your right cote Thanks

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.