Reputation: 39
I'm having issues referring to time in a while loop. Currently this is my code
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"))
So what I'm trying to do is have the while loop perform between the autoStart time and the autoStop time. If it is AFTER autoStop, I want the while loop to be broken. If it helps, this is being implemented for a light routine in which the lights only operate between 8am (autoStart) and 8pm (autoStop) but for the sake of waiting to see if it works, I'm adjusting autoStop to just one minute ahead of the current time.
I cannot seem to get unstuck from the loop and it is driving me insane because it should be fairly simple. Thanks in advance.
Upvotes: 1
Views: 49
Reputation: 39
I solved my own issue using a third time reference to iterate through my while loop
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
That way I can keep track of the current time through each iteration.
Upvotes: 0
Reputation: 1373
You should use a flag:
flag = False
while flag = False:
if condtion:
flag = True
Also check this file. It's a Django view but it might help you
Upvotes: 1