0
0
Pythonprogramming~10 mins

Instance attributes 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 create an instance attribute named 'color' with value 'red'.

Python
class Car:
    def __init__(self):
        self.[1] = 'red'

my_car = Car()
print(my_car.color)
Drag options to blanks, or click blank then click option'
Amodel
Bspeed
Ccolor
Dbrand
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different attribute name like 'speed' or 'model' instead of 'color'.
Forgetting to use 'self.' before the attribute name.
2fill in blank
medium

Complete the code to assign the instance attribute 'name' from the constructor parameter.

Python
class Person:
    def __init__(self, name):
        self.[1] = name

p = Person('Alice')
print(p.name)
Drag options to blanks, or click blank then click option'
Aid
Bage
Cgender
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different attribute name like 'age' or 'id' instead of 'name'.
Not using 'self.' before the attribute name.
3fill in blank
hard

Fix the error by completing the code to correctly access the instance attribute 'height'.

Python
class Tree:
    def __init__(self, height):
        self.height = height

my_tree = Tree(10)
print(my_tree.[1])
Drag options to blanks, or click blank then click option'
Aheight
Bsize
Cheigth
DHeight
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect capitalization like 'Height'.
Misspelling the attribute name as 'heigth'.
Using a different attribute name like 'size'.
4fill in blank
hard

Fill both blanks to create an instance attribute 'age' and assign it the value passed as parameter.

Python
class Animal:
    def __init__(self, [1]):
        self.[2] = age

pet = Animal(5)
print(pet.age)
Drag options to blanks, or click blank then click option'
Aage
Bname
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and attribute like 'name' and 'age'.
Forgetting to use 'self.' before the attribute name.
5fill in blank
hard

Fill all three blanks to create an instance attribute 'title' from the parameter and print it.

Python
class Book:
    def __init__(self, [1]):
        self.[2] = [3]

novel = Book('1984')
print(novel.title)
Drag options to blanks, or click blank then click option'
Atitle
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names like 'name' instead of 'title'.
Not using 'self.' before the attribute name.