Complete the code to define a method that prints a greeting.
class Animal: def speak(self): print([1]) pet = Animal() pet.speak()
The speak method should print the string Hello!. Using quotes around the string is necessary.
Complete the code to override the speak method in the Dog class.
class Animal: def speak(self): print("Some sound") class Dog(Animal): def speak(self): print([1]) pet = Dog() pet.speak()
The Dog class overrides speak to print "Woof!". The string must be quoted.
Fix the error in the code to call the speak method of the Cat class.
class Animal: def speak(self): print("Some sound") class Cat(Animal): def speak(self): print("Meow!") pet = Cat() pet.[1]()
The method to call is speak. Other names do not exist and cause errors.
Fill both blanks to create a list of animals and call their speak methods.
class Animal: def speak(self): print("Some sound") class Bird(Animal): def speak(self): print("Chirp!") animals = [[1], [2]] for pet in animals: pet.speak()
The list animals contains an Animal and a Bird instance. Both have a speak method.
Fill both blanks to create a dictionary mapping animal names to their sounds using polymorphism.
class Animal: def speak(self): return "Some sound" class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" pets = [Dog(), Cat()] sounds = { = {pet.{BLANK_2}}(): pet.{{BLANK_2}}() for pet in pets}
The dictionary is created with curly braces {}. The keys and values come from calling speak() on each pet.