Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an object of class Car named my_car.
Python
class Car: def __init__(self, brand): self.brand = brand my_car = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call a method on the class instead of creating an object.
Assigning a string directly to the class name.
Adding extra parentheses after object creation.
✗ Incorrect
To create an object, call the class name with parentheses and pass required arguments. Here,
Car('Toyota') creates an object with brand 'Toyota'.2fill in blank
mediumComplete the code to access the color attribute of the car object.
Python
class Car: def __init__(self, color): self.color = color car = Car('red') print(car.[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different attribute name than defined.
Capitalizing the attribute name incorrectly.
Trying to access an attribute that does not exist.
✗ Incorrect
To access an attribute, use the object name followed by a dot and the attribute name exactly as defined. Here,
car.color gives 'red'.3fill in blank
hardFix the error in the code to correctly create an object of class Person.
Python
class Person: def __init__(self, name): self.name = name person = Person[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets instead of parentheses.
Adding extra parentheses after the argument list.
Passing a tuple with a comma when not needed.
✗ Incorrect
To create an object, call the class with parentheses and pass arguments.
Person('Alice') is correct. Using square brackets or extra parentheses causes errors.4fill in blank
hardFill both blanks to create a dictionary with keys as names and values as ages from the people list.
Python
people = [('Alice', 30), ('Bob', 25), ('Cara', 27)] ages = {person[1]: person[2] for person in people}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=>' instead of ':' in dictionary comprehension.
Mixing up indices for name and age.
Using parentheses instead of square brackets for tuple access.
✗ Incorrect
To create a dictionary comprehension, use
{key: value for item in list}. Here, person[0] is the name (key) and person[1] is the age (value), separated by a colon. The full comprehension is {person[0]: person[1] for person in people}.5fill in blank
hardFill all three blanks to create a list of Person objects from the names list.
Python
class Person: def __init__(self, name): self.name = name names = ['Anna', 'Ben', 'Cara'] people = [[1]([2]) for [3] in names]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling
Person() without arguments.Using a different variable name inside the call than in the loop.
Adding parentheses after the class name in the options.
✗ Incorrect
To create objects in a list comprehension, call the class with the loop variable. Here,
Person(n) for n in names creates objects for each name.