Editor

Fix the Inheritance Bug

Intermediate · Fix the bug · from Python Inheritance: Subclasses, Polymorphism, and super()

From the lesson: Python Inheritance: Subclasses, Polymorphism, and super()

Think about how scientists classify animals. A Dog is a type of Mammal, which is a type of Animal. Each level inherits traits from the one above it: all animals breathe, all mammals are warm-blooded, and all dogs bark.

Read the lesson

This code has a bug causing an AttributeError. Fix it so it prints:

Buddy (Golden Retriever) - Friendly
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed, temperament):
self.breed = breed
self.temperament = temperament
def __str__(self):
return f'{self.name} ({self.breed}) - {self.temperament}'
dog = Dog('Buddy', 'Golden Retriever', 'Friendly')
print(dog)

More Inheritance exercises

All Python exercises · 50 practice problems · Object-Oriented Programming quiz