Printing the current minute in a loop with python
<p>I'm using python 3 and trying to create a script that runs constantly, and at some time, execute a specific code.
The code i have so far, verifies the current minute, and if it's above a given minute, it print's a message, otherwise, it prints the current minute and waits 5 seconds and try again.
The problem is that it only prints the minute the code started.</p>

<pre><code>import time
from datetime import datetime
now = datetime.now()
hour = now.hour
minute = now.minute
L=1

while (L == 1):
    if minute &gt; 39:
        print ("It's past "+str(hour)+":"+str(minute))
        L = 2
    else:
        print(str(minute))
        time.sleep(5)
</code></pre>

<p>Some programming pointers: </p>

<p>1) To make a constant loop use the following construct:</p>

<pre><code>while (True):
   if (...):
       ....
       break
</code></pre>

<p>2) The time stored in your "now" variable is static must be updated with the new time within the loop:</p>

<pre><code>while (True):
    now = datetime.now()
</code></pre>

<p>So:</p>

<pre><code>while True:
    now = datetime.now()
    if now.minute &gt; 39:
        print "Hour, Minute:", now.hour, now.minute
        print "All done!"
        break 
    else:
        print ""Minute, second:", now.minute, now.second
    time.sleep(5)
</code></pre>

<p>3) In "real life" (TM) calculate the time you want to wait until your event and sleep that long.</p>

<pre><code>now = time.now()
if now.minute &gt;= 39:
    minutesToEvent  = 0
else:
    minutesToEvent = 39 - now.minute
print "Sleep seconds to next event:", minutesToEvent * 60
sleep(minutesToEvent * 60)
</code></pre>

python