python(红绿灯)

# Author: Sooele

import time  #时间模块
import threading  #线程模块

event = threading.Event()


def lighter():
    count = 0
    while True:
        if count >20 and count < 30: #改成红灯
            event.clear() #把标志位清了
            print("\033[41;1mred light is on....\033[0m")
        elif count >30:
            event.set() #变绿灯
            count = 0
        else:
            print("\033[42;1mgreen light is on....\033[0m")
        time.sleep(1)
        count +=1


light = threading.Thread(target=lighter,)
light.start()

发表评论