我正在编写一个程序,它使用while循环来检查它是否是某个特定的时间,如果是那个特定的时间,它将打印一条语句。我已经设置了所有的if语句,但是一旦程序启动,时间就不会更新(如果我在下午6点启动程序,它将始终为本地时间输出下午6点)。有没有办法在while循环中更新时间
我试图研究datetime的一些附加函数/方法,但从我所看到的情况来看,我无法找到任何在程序运行时更新时间的函数/方法。此外,我在stackoverflow上发现的有关datetime和locale time的论坛通常只解释如何获得一次locale time(例如,Python datetime module current time in HR:MIN:SEC)。我发现其他关于语言环境时间的论坛也倾向于使用不同的语言,特别是C#和PHP。请纠正我,如果有另一个论坛,回答我的问题
from datetime import date
from datetime import time
from datetime import datetime
import time
import webbrowser
now = datetime.now()
sleep = False
today = date.today()
roundCheck = 0
print("Awaiting time check...")
while sleep != True:
print('Up here...')
if roundCheck != 0:
print('Stuck in time...')
time.sleep(60)
print('Time is done')
if str(now.strftime('%H')) == '20' and str(now.strftime('%M')) == '05':
print('Now the while loop will end')
sleep = True
roundCheck = 1
print('Another round has passed')
print('Outside while loop')
当时间为20:05时,sleep应设置为true,并且可以执行while循环外的print语句。但是,当我在更早的时间(例如20:00)启动程序时,它只使用该时间检查now.strftime()
now
永不改变。只需将now = datetime.now()
放入while循环即可相关问题 更多 >
编程相关推荐