Python 而循环指的是时间
我在一段时间的循环中遇到了时间问题。目前这是我的代码Python 而循环指的是时间,python,time,while-loop,Python,Time,While Loop,我在一段时间的循环中遇到了时间问题。目前这是我的代码 def autoRoutine(): now = datetime.datetime.now().time() autoStart = now.replace(hour=8, minute=0) stoptime = datetime.datetime.now().time() autoStop = stoptime.replace(hour=12, minute=4) while (now <
def autoRoutine():
now = datetime.datetime.now().time()
autoStart = now.replace(hour=8, minute=0)
stoptime = datetime.datetime.now().time()
autoStop = stoptime.replace(hour=12, minute=4)
while (now <= autoStop):
print("the lights are starting")
time.sleep (1.0)
if (now > autoStop):
break
print(autoStart.strftime("%H:%M"))
def autoRoutine():
now=datetime.datetime.now().time()
autoStart=now.replace(小时=8,分钟=0)
stoptime=datetime.datetime.now().time()
自动停止=停止时间。更换(小时=12,分钟=4)
而(现在为自动停止):
打破
打印(autoStart.strftime(“%H:%M”))
所以我要做的是让while循环在autoStart时间和autoStop时间之间执行。如果是在自动停止之后,我希望while循环被中断。如果有帮助的话,这是为一个只在上午8点(autoStart)到晚上8点(autoStop)之间工作的灯光例行程序实现的,但是为了等待它是否工作,我将autoStop调整为仅比当前时间提前一分钟
我似乎无法摆脱循环,这让我发疯,因为它应该是相当简单的。提前感谢。您应该使用标志:
flag=False
当flag=False时:
如果条件:
flag=True
我通过第三次引用迭代while循环解决了自己的问题
while (keepprint == False):
nowloop = datetime.datetime.now().time()
print (nowloop)
print (autoStop)
print("the lights are starting")
time.sleep (1.0)
if (nowloop >= autoStop):
keepprint = True
这样,我就可以在每次迭代中跟踪当前时间。不幸的是,我仍然卡在while循环中。此外,当我运行代码时,链接中断,我得到以下输出: