Printing the current minute in a loop with python
Question: 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.

    
    
    import time
    from datetime import datetime
    now = datetime.now()
    hour = now.hour
    minute = now.minute
    L=1
    
    while (L == 1):
        if minute > 39:
            print ("It's past "+str(hour)+":"+str(minute))
            L = 2
        else:
            print(str(minute))
            time.sleep(5)
    


Answer: Some programming pointers:

1) To make a constant loop use the following construct:

    
    
    while (True):
       if (...):
           ....
           break
    

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

    
    
    while (True):
        now = datetime.now()
    

So:

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

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

    
    
    now = time.now()
    if now.minute >= 39:
        minutesToEvent  = 0
    else:
        minutesToEvent = 39 - now.minute
    print "Sleep seconds to next event:", minutesToEvent * 60
    sleep(minutesToEvent * 60)
    

