Page 1 of 1

PLS help me WITH this loop

Posted: Thu Jul 16, 2020 2:00 am
by spegy

Code: Select all

import datetime
x = datetime.datetime.now()
time=x
if x == "2020-11-02 8:00:00.000000":
    print('pi')
else:
    print('not the right day')
pls help me loop this. the loop would need to occur whenever it is not the right time so the loop would have to be in else when it is not the right time so it just keeps repeating itself until it is the right time

Re: PLS help me WITH this loop

Posted: Thu Jul 16, 2020 7:27 am
by B.Goode
Welcome to the Raspbeery Pi forums.
spegy wrote:
Thu Jul 16, 2020 2:00 am

Code: Select all

import datetime
x = datetime.datetime.now()
time=x
if x == "2020-11-02 8:00:00.000000":
    print('pi')
else:
    print('not the right day')
pls help me loop this. the loop would need to occur whenever it is not the right time so the loop would have to be in else when it is not the right time so it just keeps repeating itself until it is the right time


You probably need to use a WHILE statement -
https://docs.python.org/3/reference/com ... html#while


(Can you be sure that your script will check the time at exactly the very precise moment you have specified?)

Re: PLS help me WITH this loop

Posted: Thu Jul 16, 2020 7:41 am
by dbrion06
Well, a while statement is a good solution, but it would eat one CPU for ca 5*30*24*3600 seconds... and keep it warm, sending many massages.

Perhaps you should
a) compute difference (in seconds) between current date and date you want to start .
b) sleep (does not eat cpu) that amount of time (either
b1) this huge amount of seconds,
or,
b2) using a for loop , sleep 1 second at the time

BTW : if you send a massage every minute, say ( "0 == currentSecond" would work) , massages would be less boring.

Edited:
you should have a look at "range", datetime substraction and for loops (specialized while loops)

Code: Select all

pydoc3 range
pydoc3 datetime 
pydoc3 datetime.date.__sub__
pydoc3 for
pydoc3 time.sleep 
would help you for details

Re: PLS help me WITH this loop

Posted: Thu Jul 16, 2020 4:09 pm
by spegy
thank you so much for your help