spegy
Posts: 2
Joined: Thu Jul 16, 2020 1:42 am

PLS help me WITH this loop

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

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: PLS help me WITH this loop

Thu Jul 16, 2020 7:27 am

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?)

dbrion06
Posts: 88
Joined: Tue May 28, 2019 11:57 am

Re: PLS help me WITH this loop

Thu Jul 16, 2020 7:41 am

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

spegy
Posts: 2
Joined: Thu Jul 16, 2020 1:42 am

Re: PLS help me WITH this loop

Thu Jul 16, 2020 4:09 pm

thank you so much for your help

Return to “Python”