Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to change the attribute 'color' of the car object to 'red'.
Python
class Car: def __init__(self, color): self.color = color car = Car('blue') car.color = [1] print(car.color)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the color name.
Trying to assign to the class instead of the object.
✗ Incorrect
Assigning 'red' to car.color changes the car's color to red.
2fill in blank
mediumComplete the method to update the 'speed' attribute of the Bike object.
Python
class Bike: def __init__(self, speed): self.speed = speed def set_speed(self, new_speed): self.speed = [1] bike = Bike(10) bike.set_speed(20) print(bike.speed)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the attribute name instead of the parameter.
Assigning a fixed number instead of the parameter.
✗ Incorrect
The method parameter new_speed holds the new value to assign to self.speed.
3fill in blank
hardFix the error in the method that increases the 'count' attribute by 1.
Python
class Counter: def __init__(self): self.count = 0 def increment(self): self.count [1] 1 counter = Counter() counter.increment() print(counter.count)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which replaces the value instead of adding.
Using '-=' or '*=' which change the value incorrectly.
✗ Incorrect
The += operator adds 1 to the current value of self.count.
4fill in blank
hardFill both blanks to create a method that doubles the 'value' attribute of the object.
Python
class Number: def __init__(self, value): self.value = value def double(self): self.value = self.value [1] [2] num = Number(5) num.double() print(num.value)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using the wrong number or variable.
✗ Incorrect
Multiplying self.value by 2 doubles its value.
5fill in blank
hardFill all three blanks to create a method that resets the 'score' attribute to zero and returns it.
Python
class Game: def __init__(self): self.score = 10 def reset_score(self): self.score = [1] return [2] player = Game() result = player.reset_score() print([3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning or printing the wrong variable.
Not resetting the attribute to zero.
✗ Incorrect
The method sets self.score to 0, returns it, and the returned value is stored in result which is printed.