0
0
Pythonprogramming~10 mins

Purpose of constructors 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 constructor method in a Python class.

Python
class Car:
    def [1](self):
        self.color = 'red'
Drag options to blanks, or click blank then click option'
A__init__
Binit
Cstart
Dconstructor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'init' without underscores
Using 'constructor' as method name
Using 'start' instead of '__init__'
2fill in blank
medium

Complete the constructor to accept a parameter and assign it to an instance variable.

Python
class Person:
    def __init__(self, name):
        self.[1] = name
Drag options to blanks, or click blank then click option'
Aself
BName
Cname
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalized 'Name' which is different
Assigning to 'self.self' or 'self.person'
Forgetting to use 'self.' before the variable
3fill in blank
hard

Fix the error in the constructor method name.

Python
class Animal:
    def [1](self):
        self.type = 'mammal'
Drag options to blanks, or click blank then click option'
A__init__
Binit__
C_init_
Dinitialize
Attempts:
3 left
💡 Hint
Common Mistakes
Using single underscores or missing underscores
Using 'initialize' instead of '__init__'
Misspelling the method name
4fill in blank
hard

Fill both blanks to create a constructor that sets two instance variables.

Python
class Book:
    def __init__(self, title, author):
        self.[1] = title
        self.[2] = author
Drag options to blanks, or click blank then click option'
Atitle
Bname
Cauthor
Dwriter
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names like 'name' or 'writer' for instance variables
Mixing up the order of variables
Forgetting 'self.' before variable names
5fill in blank
hard

Fill all three blanks to create a constructor that initializes three instance variables.

Python
class Laptop:
    def __init__(self, brand, model, price):
        self.[1] = brand
        self.[2] = model
        self.[3] = price
Drag options to blanks, or click blank then click option'
Abrand
Bmodel
Cprice
Dcost
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cost' instead of 'price' for the last variable
Mixing variable names or forgetting 'self.'
Assigning wrong parameters to variables