Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

python - Print something if the date changes

What exactly I am trying to do here is when Tomorrow comes (00:00) it should print Yes

import datetime
from datetime import date

r = True
rr = True
while r:
    Today_Date = date.today()
    while rr:
        Tomorrow_Date = datetime.date.today() + datetime.timedelta(days=1)
        if Today_Date == Tomorrow_Date:
            print("Yes")
question from:https://stackoverflow.com/questions/65838312/print-something-if-the-date-changes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The way you have it now, your rr while loop will continually run and update Tomorrow_Date, and when the day rolls over, it will update before it has a chance to be compared to Today_Date. You should set both Today_Date and Tomorrow_Date outside that loop, and only update them when the dates change.

This should do the trick:

If you want two loops for other reasons:

import datetime

r = True
while r:
  rr = True
    Tomorrow_Date = datetime.date.today() + datetime.timedelta(days=1)
    while rr:
        if datetime.date.today() >= Tomorrow_Date:
            print("Yes")
            rr = False

Or as a single loop:

import datetime

r = True
Tomorrow_Date = datetime.date.today() + datetime.timedelta(days=1)
while r:
    if datetime.date.today() >= Tomorrow_Date:
        print("Yes")
        Tomorrow_Date = datetime.date.today() + datetime.timedelta(days=1)

It might be a good idea to add a time.sleep() in to slow down the loops, depending on how accurate you need to be also.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...