Chapters
Python114 chapters

Exercises

self

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

The method is missing self, so calling it fails. Fix it.

Python
class Dog:
    def speak():
        return "woof"

print(Dog().speak())
Exercise 2

This sets a local that is thrown away. Make it set the object's value instead.

Python
class Counter:
    def __init__(self):
        self.value = 0

    def set_to_99(self):
        value = 99

c = Counter()
c.set_to_99()
print(c.value)
Exercise 3

Add older_than so one dog can be compared with another.

Python
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

print(Dog("Rex", 5).older_than(Dog("Fido", 3)))