Hello,
I am writing a program in python and I would like to make sth like this:
“execute something when it’s X o’clock, e.g.: print “hi” when it’s 17:56”
What I tried is:
import datetime
if datetime.datetime.now().strftime("%H:%M") == "17:55":
print("hi")
However, this just passed and ignored the if statement - the code ended immediately after it was started and it did not print “hi” at 17:55
Does anyone know how to deal with this?
Thank you.
As you have noticed, when code is run it finishes just after it starts. To be precise there’s just one check of the if condition and as it wasn’t evaluating to True at that time, program finished without printing anything. Therefore what is required is to give code a chance to check it more than once. Take a look at loops in python for that.