Skip to content

Instantly share code, notes, and snippets.

@TutorialDoctor
Created August 25, 2015 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TutorialDoctor/1e65fbe19e5a3acbb3d3 to your computer and use it in GitHub Desktop.
Save TutorialDoctor/1e65fbe19e5a3acbb3d3 to your computer and use it in GitHub Desktop.
The beginning of a simple traffic light program
# Traffic Light
# By the Tutorial Doctor
# THE CLASS
#--------------------------------------------------
class TrafficLight():
GREEN = [1,0,0]
YELLOW = [0,1,0]
RED = [0,0,1]
def __init__(self):
self.state = [0,0,0]
def signal(self):
if self.state==TrafficLight.RED:
return 'stop'
elif self.state==TrafficLight.GREEN:
return 'go'
elif self.state==TrafficLight.YELLOW:
return 'slow down'
def switchIndex(self):
for switch in self.state:
if switch==1:
return self.state.index(switch)
def __str__(self):
return self.signal()
#--------------------------------------------------
# IMPLEMENTATION
#--------------------------------------------------
light = TrafficLight()
print light.state
light.state=TrafficLight.RED
print light.signal()
print light.switchIndex()
print help(light)
#--------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment