Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python classes and objects code

Practice defining a class, initializing instance state, adding methods, and extending a base class.

Beginner ~8 min read

Challenge 1

Complete the class definition so creating Dog() works.

python Your answer
class Dog:

pet = Dog()
print(type(pet).__name__)

Press Ctrl⌘Enter to check your answer

Challenge 2

Store the supplied name on the instance.

python Your answer
class Dog:
    def __init__(self, name):

pet = Dog("Fido")
print(pet.name)

Press Ctrl⌘Enter to check your answer

Challenge 3

Complete the method so it increments the counter value by one.

python Your answer
class Counter:
    def __init__(self):
        self.value = 0

    def increment(self):

counter = Counter()
counter.increment()
print(counter.value)

Press Ctrl⌘Enter to check your answer

Challenge 4

Use the base-class initializer so the Dog instance receives name.

python Your answer
class Animal:
    def __init__(self, name):
        self.name = name

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

pet = Dog("Fido", "collie")
print(pet.name, pet.breed)

Press Ctrl⌘Enter to check your answer