-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraffic_light.py
More file actions
22 lines (18 loc) · 778 Bytes
/
traffic_light.py
File metadata and controls
22 lines (18 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""This module defines the class representing a traffic light."""
class TrafficLight(object):
"""This class represents a traffic light that can change state."""
RED = 1
GREEN = 2
def __init__(self, color):
"""Creates a new traffic light object that is initially set to color."""
assert color == TrafficLight.RED or color == TrafficLight.GREEN
self._color = color
def flip_color(self):
"""Change the color of the traffic light."""
if self._color == TrafficLight.RED:
self._color = TrafficLight.GREEN
elif self._color == TrafficLight.GREEN:
self._color = TrafficLight.RED
def light_color(self):
"""Get the current state of the traffic light."""
return self._color