0
0
Pythonprogramming~10 mins

Public 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 a public attribute named name in the Person class.

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

p = Person("Alice")
print(p.name)
Drag options to blanks, or click blank then click option'
Aage
Bself
Cname
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' as a parameter name instead of 'name'.
Forgetting to pass the parameter when creating the object.
2fill in blank
medium

Complete the code to access the public attribute color of the Car object.

Python
class Car:
    def __init__(self, color):
        self.color = color

my_car = Car("red")
print(my_car.[1])
Drag options to blanks, or click blank then click option'
Amodel
Bspeed
Cowner
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access an attribute that does not exist.
Using parentheses like a method call instead of attribute access.
3fill in blank
hard

Fix the error in the code by completing the blank to correctly set the public attribute age.

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

cat = Animal(3)
print(cat.age)
Drag options to blanks, or click blank then click option'
Ayears
Bage
Cold
Dage_
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different attribute name than the one accessed.
Misspelling the attribute name.
4fill in blank
hard

Fill both blanks to create a public attribute height and print its value.

Python
class Tree:
    def __init__(self, [1]):
        self.[2] = height

pine = Tree(15)
print(pine.height)
Drag options to blanks, or click blank then click option'
Aheight
Bsize
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter and attribute.
Using a name not matching the print statement.
5fill in blank
hard

Fill all three blanks to create a public attribute width, set it, and print it.

Python
class Box:
    def __init__(self, [1]):
        self.[2] = width

b = Box(10)
print(b.[3])
Drag options to blanks, or click blank then click option'
Awidth
Dheight
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for parameter, attribute, or access.
Using 'height' instead of 'width'.