Computer scienceProgramming languagesPythonObject-oriented programmingMethods

Methods and attributes

Lightbulb

Report a typo

Here's a class Lightbulb. It has only one attribute that represents its state: whether it's on or off.

Create a method change_state that changes the state of the lightbulb. In other words, the method turns the light on or off depending on its current state. The method doesn't take any arguments (except for self) and prints a corresponding message: Turning the light on if it was off, and Turning the light off if it was on.

Inside the method, you are also supposed to change the value of the attribute state. You do not need to call the method yourself, just implement it.

To produce the correct result, inside the change_state method check the current state of the lightbulb by accessing the state attribute.
Write a program in Python 3
class Lightbulb:
def __init__(self):
self.state = "off"

# create method change_state here
___

Create a free account to access the full topic