how could i make the function to do the colours red, yellow and green for 5 sec delays and make the loop keep on going?

def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
    while True:
        import time
        time.sleep(5)

hi,
as Ene Uran mentioned, not sure what GUI toolkit this is but I'll try to help anyway, I think I get the overall idea :)
Just a note: put the import statements in the beginning of the program.

It's something like this:

import time

def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
    
    color = "red"
    while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            if color == "red":
                color = "amber"
            elif color == "amber":
                color = "green"
            elif color == "green":
                color = "red"

Note I think this should work but I'm not sure because I haven't tested it :)

hope this helps :)

Edited 7 Years Ago by masterofpuppets: Forgot code tags AGAIN

sorry I forgot that when you change the color you need to set the color of the previous light to black again:

while True:
    for light in [ red, amber, green ]:
        light.setFill( color )
        # not sure what the update function for the screen is but you need to call it before you call time.sleep()
        time.sleep(5)
        light.setFill( "black" )
        if color == "red":
            color = "yellow"
        elif color == "yellow":
            color = "green"
        elif color == "green":
            color = "red"

you could simplify the if..elif to this

changeColor = { "red":"yellow", "yellow":"green", "green":"red" }
while True:
    for light in [ red, amber, green ]:
        light.setFill( color )
        # not sure what the update function for the screen is but you need to call it before you call time.sleep()
        time.sleep(5)
        light.setFill( "black" )
        color = changeColor[ color ]

Edited 7 Years Ago by masterofpuppets: n/a

sorry I forgot that when you change the color you need to set the color of the previous light to black again:

while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            light.setFill( "black" )
            if color == "red":
                color = "amber"
            elif color == "amber":
                color = "green"
            elif color == "green":
                color = "red"

i get this error... when i try and run the code...

def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
    while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            light.setFill( "black" )
            if color == "red":
                color = "amber"
            elif color == "amber":
                color = "green"
            elif color == "green":
                color = "red"

what's the error message?

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
trafficLights()
File "C:\Documents and Settings\Compaq_Owner\My Documents\Python\week08.py", line 19, in trafficLights
light.setFill( color )
UnboundLocalError: local variable 'color' referenced before assignment

ah yes I think I haven't defined color :)

try this:

import time
 
def trafficLights():
    win = GraphWin()
    red = Circle(Point(100, 50), 20)
    red.setFill("black")
    red.draw(win)
    amber = Circle(Point(100, 100), 20)
    amber.setFill("black")
    amber.draw(win)
    green = Circle(Point(100, 150), 20)
    green.setFill("black")
    green.draw(win)
 
    color = "red"
    
    changeColor = { "red":"yellow", "yellow":"green", "green":"red" }
    while True:
        for light in [ red, amber, green ]:
            light.setFill( color )
            # not sure what the update function for the screen is but you need to call it before you call time.sleep()
            time.sleep(5)
            light.setFill( "black" )
            color = changeColor[ color ]

Edited 7 Years Ago by masterofpuppets: n/a

You are welcome to do one, or hire a programmer if you need one for real. But learning requires for you to do honest try yourself. Also the rules of Daniweb tells the same. Also you are welcome to start new threads, but do not continue old threads if your answer does not contribute to original messages left unanswered. As it says at bottom:

This question has already been solved: Start a new discussion instead

Edited 3 Years Ago by pyTony

This question has already been answered. Start a new discussion instead.