如何在特定时间后停止此 while 循环?
how do I stop this while loop after a specific time?
我目前正在制作闪电代码,但我在 while 循环中遇到了一些问题。
我想在我设置的特定时间后停止 While 循环。
这是代码。
class Light:
def __init__(self):
self.work_flag = False
def start(self):
self.work_flag = True
def stop(self):
self.work_flag = False
def mode_1(self):
print("turn on red+green+blue light")
sleep(0.5)
while self.work_flag:
print("turn on red light")
sleep(0.3)
print("turn on green light")
sleep(0.3)
print("turn on blue light")
sleep(0.3)
print("turn on red+green light")
sleep(0.3)
print("turn on red+blue light")
sleep(0.3)
print("turn on green+blue light")
sleep(0.3)
light = Light()
def light_start():
light.start()
light.mode_1()
def light_stop():
light.stop()
light_start()
sleep(5)
light_stop()
也许你应该添加 elapsed_time 方法。它会等到 'while' 完成所有工作,但我认为它一点也不差。
import time
class Light:
def __init__(self):
self.work_flag = False
def start(self):
self.startTime = time.time() # start time
self.work_flag = True
def stop(self):
self.work_flag = False
def mode_1(self):
self.endTime = time.time() # end time
elapsedTime = self.endTime - self.startTime # elapsed time
print("turn on red+green+blue light")
time.sleep(0.5)
#if elapsedTime > 5: the work is done.
while self.work_flag and elapsedTime < 5: # 5 seconds
self.endTime = time.time() # end time
elapsedTime = self.endTime - self.startTime # elapsed time
print(elapsedTime)
print("turn on red light")
time.sleep(0.3)
print("turn on green light")
time.sleep(0.3)
print("turn on blue light")
time.sleep(0.3)
print("turn on red+green light")
time.sleep(0.3)
print("turn on red+blue light")
time.sleep(0.3)
print("turn on green+blue light")
time.sleep(0.3)
light = Light()
def light_start():
light.start()
light.mode_1()
def light_stop():
light.stop()
start = time.time()
light_start()
time.sleep(5)
light_stop()
我目前正在制作闪电代码,但我在 while 循环中遇到了一些问题。 我想在我设置的特定时间后停止 While 循环。 这是代码。
class Light:
def __init__(self):
self.work_flag = False
def start(self):
self.work_flag = True
def stop(self):
self.work_flag = False
def mode_1(self):
print("turn on red+green+blue light")
sleep(0.5)
while self.work_flag:
print("turn on red light")
sleep(0.3)
print("turn on green light")
sleep(0.3)
print("turn on blue light")
sleep(0.3)
print("turn on red+green light")
sleep(0.3)
print("turn on red+blue light")
sleep(0.3)
print("turn on green+blue light")
sleep(0.3)
light = Light()
def light_start():
light.start()
light.mode_1()
def light_stop():
light.stop()
light_start()
sleep(5)
light_stop()
也许你应该添加 elapsed_time 方法。它会等到 'while' 完成所有工作,但我认为它一点也不差。
import time
class Light:
def __init__(self):
self.work_flag = False
def start(self):
self.startTime = time.time() # start time
self.work_flag = True
def stop(self):
self.work_flag = False
def mode_1(self):
self.endTime = time.time() # end time
elapsedTime = self.endTime - self.startTime # elapsed time
print("turn on red+green+blue light")
time.sleep(0.5)
#if elapsedTime > 5: the work is done.
while self.work_flag and elapsedTime < 5: # 5 seconds
self.endTime = time.time() # end time
elapsedTime = self.endTime - self.startTime # elapsed time
print(elapsedTime)
print("turn on red light")
time.sleep(0.3)
print("turn on green light")
time.sleep(0.3)
print("turn on blue light")
time.sleep(0.3)
print("turn on red+green light")
time.sleep(0.3)
print("turn on red+blue light")
time.sleep(0.3)
print("turn on green+blue light")
time.sleep(0.3)
light = Light()
def light_start():
light.start()
light.mode_1()
def light_stop():
light.stop()
start = time.time()
light_start()
time.sleep(5)
light_stop()