Traffic lights are signaling devices positioned at road intersections, pedestrian crossings, and other locations to control flows of traffic. Traffic lights alternate the right way accorded to users by displaying lights of a standard color (red, orange and green) following a universal color code.
In the typical sequence of color phases:
The green light allows traffic to proceed in the direction denoted, if it is safe to do so and there is room on the other side of the intersection.
The orange light warns that the signal is about to change to red. Actions required by drivers on a orange light vary, with some jurisdictions requiring drivers to stop if it is safe to do so, and others allowing drivers to go through the intersection if safe to do so.
The red signal prohibits any traffic from proceeding.
Define a class TrafficLight that can be used to represent traffic lights in Python. At any point in time, each traffic light is in one of three possible states: red, orange or green. The class TrafficLight must support at least the following methods:
An initialisation method __init__ that may take the initial state of a traffic light (str; default value: red). The only possible states are red, orange and green (lowercase).
A method __str__ that returns a string representation (str) of the object, containing the current state of the traffic light.
A method __repr__ that returns a string representation (str) of the object, containing a Python expression that indicates how a traffic light can be created that has the same state as the current state of the object on which the method is called.
A method next that puts the traffic light in the next state: after red comes green, after green comes orange, and after orange comes red.
No arguments can be passed to the methods __str__, __repr__ and next.
>>> light1 = TrafficLight()
>>> light1
TrafficLight('red')
>>> licht2 = TrafficLight('green')
>>> print(light1, light2)
red green
>>> light1.next()
>>> print(light1, light2)
green green
>>> light1.next()
>>> light1.next()
>>> light2.next()
>>> print(light1, light2)
red orange