0
0
Pythonprogramming~10 mins

Constructor parameters 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 that takes one parameter 'name'.

Python
class Person:
    def __init__(self, [1]):
        self.name = name
Drag options to blanks, or click blank then click option'
Aname
Bself
Cage
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' as a parameter name other than the first one.
Forgetting to include the parameter in the constructor.
2fill in blank
medium

Complete the code to assign the parameter 'age' to the instance variable.

Python
class Person:
    def __init__(self, age):
        self.age = [1]
Drag options to blanks, or click blank then click option'
Aage
Bself.age
CPerson.age
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning self.age to itself instead of the parameter.
Using the class name instead of the parameter.
3fill in blank
hard

Fix the error in the constructor parameter to correctly accept two parameters: 'name' and 'age'.

Python
class Person:
    def __init__(self, [1]):
        self.name = name
        self.age = age
Drag options to blanks, or click blank then click option'
Aname age
Bname; age
Cname, age
Dself, name, age
Attempts:
3 left
💡 Hint
Common Mistakes
Separating parameters with spaces or semicolons instead of commas.
Including 'self' again in the parameter list.
4fill in blank
hard

Fill both blanks to create a constructor that assigns 'name' and 'age' parameters to instance variables.

Python
class Person:
    def __init__(self, name, age):
        self.name = [1]
        self.age = [2]
Drag options to blanks, or click blank then click option'
Aname
Bage
Cself.name
Dself.age
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning instance variables to themselves instead of parameters.
Mixing up the parameter names.
5fill in blank
hard

Fill all three blanks to create a constructor that assigns 'name', 'age', and 'city' parameters to instance variables.

Python
class Person:
    def __init__(self, name, age, city):
        self.name = [1]
        self.age = [2]
        self.city = [3]
Drag options to blanks, or click blank then click option'
Aname
Bage
Ccity
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of parameter names on the right side.
Mixing up parameter names and instance variables.