educative.io

Educative

Animal class polymorphism challenge

Hi,

No matter what I write, the first 2 test cases won’t pass. Please let me know what’s wrong with below code.
This is regarding the challenge in Python basic Module 2 challenge for learning polymorphism.

class Animal:
def init(self, name, sound):
self.name = name
self.sound = sound

def Animal_details():
    print("Name:", self.name)
    print("Sound:", self.sound)

class Dog(Animal):
def init(self, name, sound, family):
super().init(name, sound)
self.family = family

def Animal_details():
    super().Animal_details()
    print("Family:", self.family)

class Sheep(Animal):
def init(self, name, sound, color):
super().init(name, sound)
self.family = color

def Animal_details():
    super().Animal_details()
    print("Color:", self.color)

Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers

Hi @nikhil_kohli
In your solution, you are not properly defining the classes Dog and Sheep. Also, you are not passing the Animal_details() function an argument of self. Other than that, in the class Sheep you are setting the self.family = color whereas it should be self.color = color instead.

Try the following solution. It will work for you.

class Animal:
    def __init__(self, name, sound):
        self.name = name
        self.sound = sound

    def Animal_details(self):
        print("Name:", self.name)
        print("Sound:", self.sound)


class Dog(Animal):
    def __init__(self, name, sound, family):
        super().__init__(name, sound)
        self.family = family

    def Animal_details(self):
        super().Animal_details()
        print("Family:", self.family)


class Sheep(Animal):
    def __init__(self, name, sound, color):
        super().__init__(name, sound)
        self.color = color

    def Animal_details(self):
        super().Animal_details()
        print("Color:", self.color)

Thanks much, need to get my eyes checked :smiley:


Course: Educative: Interactive Courses for Software Developers
Lesson: Educative: Interactive Courses for Software Developers