While循环指的是tim

2024-09-28 01:25:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一段时间的循环中遇到了时间问题。目前这是我的代码

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"))

所以我要做的是让while循环在autoStart时间和autoStop时间之间执行。如果是在自动停止之后,我希望while循环被中断。如果有帮助的话,这是为一个只在上午8点(autoStart)到晚上8点(autoStop)之间工作的灯光例行程序实现的,但是为了等待它是否工作,我将autoStop调整为仅比当前时间提前一分钟

我似乎无法摆脱循环,这让我发疯,因为它应该是相当简单的。提前谢谢


Tags: 代码datetimetimedef时间nowreplaceprint
2条回答

我使用第三次引用来迭代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

这样我就可以在每次迭代中跟踪当前时间

您应该使用一个标志:

flag = False
while flag = False:
    if condtion:
        flag = True

Also check this file. It's a Django view but it might help you

相关问题 更多 >

    热门问题