Animal voice class
Define the basic class with the name of the Make_Sound method. Then create DOG and CAT classes to inherit the Animal and each animal has its own sound.
class Animal:
def make_sound(self):
print("صدای حیوان")
class Dog(Animal):
def make_sound(self):
print("hp hp")
class Cat(Animal):
def make_sound(self):
return "me me"
m=Dog()
m.make_sound()
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
if self.name == 'dog':
return f"{self.name} : woof woof"
elif self.name == 'cat':
return f"{self.name} : meow meow"
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
class Cat(Animal):
def __init__(self, name):
super().__init__(name)
dog1 = Dog('dog')
cat1 = Cat('cat')
print(dog1.make_sound())
print(cat1.make_sound())
Submitting answers is currently unavailable.
You must be logged in to access this section.
Login/Sign up If you don’t understand the exercise or can’t solve it for any reason, that’s completely
normal—don’t worry 😊
Try checking out easier exercises and reviewing different answers
submitted by others. Gradually, you can move on to more challenging exercises. Also, your answer
might be correct even if it’s different from others.