Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set a default value for the 'color' attribute in the constructor.
Python
class Car: def __init__(self, color=[1]): self.color = color my_car = Car() print(my_car.color)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around string default values.
Using a variable name without quotes as a default value.
✗ Incorrect
The default value for the 'color' parameter should be a string, so it must be enclosed in quotes.
2fill in blank
mediumComplete the constructor to give 'speed' a default value of 0.
Python
class Bike: def __init__(self, speed=[1]): self.speed = speed my_bike = Bike() print(my_bike.speed)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string '0' instead of the number 0.
Leaving the default value empty.
✗ Incorrect
The default value for 'speed' should be the number 0, not a string.
3fill in blank
hardFix the error in the constructor by completing the default value for 'name'.
Python
class Person: def __init__(self, name=[1]): self.name = name p = Person() print(p.name)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using None without handling it in the code.
Using a number 0 as a name.
✗ Incorrect
The default name should be a string, so "Unknown" is the best choice.
4fill in blank
hardFill both blanks to set default values for 'width' and 'height' in the constructor.
Python
class Rectangle: def __init__(self, width=[1], height=[2]): self.width = width self.height = height rect = Rectangle() print(rect.width, rect.height)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of numbers for default sizes.
Setting default sizes to 0 which might represent no size.
✗ Incorrect
Both width and height should default to the number 1 for a simple rectangle size.
5fill in blank
hardFill all three blanks to set default values for 'title', 'author', and 'pages' in the constructor.
Python
class Book: def __init__(self, title=[1], author=[2], pages=[3]): self.title = title self.author = author self.pages = pages book = Book() print(book.title, book.author, book.pages)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using None for strings without handling it.
Putting numbers in quotes.
✗ Incorrect
The title and author are strings with default names, and pages is a number defaulting to 0.