简体   繁体   中英

I want to run a infinite loop and check for a specific condition at the set minute how can i do it

from datetime import datetime
check_min=[9,15,25,35,45,55]
check_sec=1
time_now=datetime.now()
min_now=time_now.minute
second_now=time_now.second
something=True
while True:
    if min_now in check_min and something==True:
        print("Yes")
        something=False
        
        
        

I am trying to run a infinite loop which checks for a specific condition in the given list of minutes.The loop is getting stuck if the programme is executed in the minutes when not in the list,How do I solve this?

You have to get min_now every time the loop repeats:

from datetime import datetime
check_min=[9,15,25,35,45,55]
check_sec=1
something=True
while True:
    time_now=datetime.now()
    min_now=time_now.minute
    second_now=time_now.second
    if min_now in check_min and something==True:
        print("Yes")
        something=False

(I put second_now in there also, but if you are not checking seconds then you don't need it. Depends on the program.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM