0
0
Pythonprogramming~10 mins

Class definition syntax 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 named Car.

Python
class [1]:
    pass
Drag options to blanks, or click blank then click option'
ACar
Bcar
Cvehicle
DCarClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for classes.
Adding parentheses after the class name when not inheriting.
2fill in blank
medium

Complete the code to add an __init__ method that takes self and color as parameters.

Python
class Car:
    def [1](self, color):
        self.color = color
Drag options to blanks, or click blank then click option'
A__init__
Bstart
Cinitialize
Dconstructor
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name like 'initialize' or 'constructor' which are not Python special methods.
Forgetting the double underscores.
3fill in blank
hard

Fix the error in the method definition to correctly define a method named describe that takes only self.

Python
class Car:
    def describe([1]):
        print(f"This car is {self.color}.")
Drag options to blanks, or click blank then click option'
Acar
Bthis
Cself
Dcls
Attempts:
3 left
💡 Hint
Common Mistakes
Using other names like 'this' or 'cls' which are not correct for instance methods.
Omitting the parameter entirely.
4fill in blank
hard

Fill both blanks to create a method named paint that changes the car's color to a new color passed as new_color.

Python
class Car:
    def [1](self, [2]):
        self.color = new_color
Drag options to blanks, or click blank then click option'
Apaint
Bcolor
Cnew_color
Dchange
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name different from the one assigned to self.color.
Using a method name that does not describe the action.
5fill in blank
hard

Fill all three blanks to define a class Person with an __init__ method that takes self, name, and age, and assigns them to instance variables.

Python
class [1]:
    def [2](self, name, age):
        self.[3] = name
        self.age = age
Drag options to blanks, or click blank then click option'
APerson
B__init__
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names for the constructor.
Assigning to a different instance variable name than the parameter.