Traffic lights in Python
Archive - Originally posted on "The Horse's Mouth" - 2010-04-13 07:48:20 - Graham EllisA really short demo (I'm short on time ... in Dublin and rushing off to do day No. 2 of a Python course!)
A loop - an anonymous list, and a modulo function that lets us cycle round and round the members of that list
for k in range(12):
light = ["red","red-an-yellow","green","yellow"][k%4]
print light
Giving me the English traffic lighht colours:
Dorothy-2: grahamellis$ python tlight
red
red-an-yellow
green
yellow
red
red-an-yellow
green
yellow
red
red-an-yellow
green
yellow
Dorothy-2: grahamellis$
Here in Dublin, they miss out the red-an-yellow - straight from red to green!