Complete the code to call the method that makes the object quack.
class Duck: def quack(self): print("Quack!") bird = Duck() bird.[1]()
The quack() method is called to make the duck quack.
Complete the code to check if an object has a 'quack' method using duck typing.
def make_it_quack(obj): if hasattr(obj, '[1]'): obj.quack() else: print("This object can't quack.")
We check if the object has a quack method to decide if it can quack.
Fix the error in the code to correctly use duck typing to call 'quack' if available.
class Dog: def bark(self): print("Woof!") pet = Dog() if hasattr(pet, '[1]'): pet.quack() else: print("Pet can't quack.")
The code checks if pet has a quack method before calling it. The blank must be 'quack' to match the method call.
Fill both blanks to create a list of objects that can quack and call their quack method.
class Duck: def quack(self): print("Quack!") class Person: def quack(self): print("I'm pretending to be a duck!") objects = [Duck(), Person(), 42] for obj in objects: if hasattr(obj, '[1]'): obj.[2]()
We check if each object has a quack method and then call quack() on it.
Fill all three blanks to create a function that calls 'quack' if available, else calls 'bark' if available, else prints a message.
def make_sound(obj): if hasattr(obj, '[1]'): obj.[2]() elif hasattr(obj, '[3]'): obj.bark() else: print("No sound method found.")
The function first checks for quack and calls it, then checks for bark and calls it, otherwise prints a message.