We use Python 3.6

go back Level: Advanced (img: AV / score: 4) level Bite 63. Use an infinite iterator to simulate a traffic light

14 out of 14 users completed this Bite ...
Will you be Pythonista #15 to crack this Bite?
Average Bite difficulty 1-10 rating: 1.0
» You can do it! 😌

Complete traffic_light using itertools.cycle to return an infinite iterator to similate a traffic light.

When called it should return a State namedtuple for each new state, here is how an islice looks:

from pprint import pprint as pp
pp(list(islice(traffic_light(), 10)))

This captures the first 10 iterated values of traffic_light into a list and pretty prints them:

[State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2)]

Good luck and have fun!

Github login button