囚犯关灯

来源:互联网 发布:stc单片机烧录工具 编辑:程序博客网 时间:2024/04/27 16:46
lamp = 'off'stop = 0class A:def __init__(self):self.haveopen_times = 0def do(self):global lamp,stopif lamp == 'off':self.haveopen_times += 1if self.haveopen_times == 99:print 'all prisoner have opened lamp'stop = 1print 'open lamp'lamp = 'on'class B:def __init__(self):self.haveopen = 0def do(self):global lampif self.haveopen ==0 and  lamp == 'on':self.haveopen = 1lamp = 'off'print 'off lamp'else :print 'can not off lamp'prisoner = [A()] + [B() for i in range(99)]import randomi = 0while not stop:i += 1print i,random.choice(prisoner).do()