0
0
Pythonprogramming~10 mins

Purpose of inheritance in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a class that inherits from another class.

Python
class Animal:
    def sound(self):
        return "Some sound"

class Dog([1]):
    pass
Drag options to blanks, or click blank then click option'
ADog
BAnimal
CSound
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name inside parentheses instead of the parent class.
Forgetting to put parentheses after the class name.
2fill in blank
medium

Complete the code to call the inherited method from the child class instance.

Python
class Animal:
    def sound(self):
        return "Some sound"

class Cat(Animal):
    pass

pet = Cat()
print(pet.[1]())
Drag options to blanks, or click blank then click option'
Asound
Bcall
Cnoise
Dmake_sound
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist in the parent class.
Trying to call the method without parentheses.
3fill in blank
hard

Fix the error in the code by completing the inheritance syntax correctly.

Python
class Vehicle:
    def move(self):
        return "Moving"

class Car[1]Vehicle):
    pass
Drag options to blanks, or click blank then click option'
A(
B:
C[
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the opening parenthesis before the parent class name.
Using square brackets or braces instead of parentheses.
4fill in blank
hard

Fill both blanks to create a child class that overrides a method from the parent class.

Python
class Bird:
    def fly(self):
        return "Flying"

class Penguin([1]):
    def fly(self):
        return [2]
Drag options to blanks, or click blank then click option'
ABird
B"Cannot fly"
C"Flying"
DAnimal
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parent class name.
Not returning a string in the overridden method.
5fill in blank
hard

Fill all three blanks to create a class hierarchy and call a method from the grandparent class.

Python
class LivingThing:
    def breathe(self):
        return "Breathing"

class Animal([1]):
    pass

class Dog([2]):
    def breathe(self):
        return super().[3]()
Drag options to blanks, or click blank then click option'
ALivingThing
BAnimal
Cbreathe
DDog
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong class names in inheritance.
Forgetting to call the method with parentheses.