0
0
Pythonprogramming~10 mins

Modifying object state 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 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'
A'green'
B'blue'
C'yellow'
D'red'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the color name.
Trying to assign to the class instead of the object.
2fill in blank
medium

Complete 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'
A20
Bnew_speed
Cself.speed
Dspeed
Attempts:
3 left
💡 Hint
Common Mistakes
Using the attribute name instead of the parameter.
Assigning a fixed number instead of the parameter.
3fill in blank
hard

Fix 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'
A-=
B=
C+=
D*=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' which replaces the value instead of adding.
Using '-=' or '*=' which change the value incorrectly.
4fill in blank
hard

Fill 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'
A*
B+
C2
Dself.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using the wrong number or variable.
5fill in blank
hard

Fill 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'
A0
Bself.score
Cresult
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Returning or printing the wrong variable.
Not resetting the attribute to zero.