Adamsmith0016 Adamsmith0016 - 5 months ago 21
Python Question

leave program running in the background and continuously check on the value of the clock

My goal is to create a program which continuously checks on the value of time,compares it to variable values (static for now: one or two or three), it will open a specific page, if it matches the value one, I still want the program to stay running till it matches two, perform the same action, same for the value of three.

1- How do I let my program run continuously while checking the clock value ?

2- How do I let the program alive to run for the second and third value ?

The program only works when I run it specifically at the exact time of one of the values...

import time
import webbrowser
print(" This started on " + time.strftime("%H:%M"))
count = 0
clock = time.strftime("%H:%M")
#values i want to assign for breaks
one = "17:15"
two = "21:15"
three = "23:00"
#continous loop
while True:
if (clock == one ):
time.sleep(2)
webbrowser.open("https://www.youtube.com/watch?v=QZbuj3RJcjI")
break
elif (clock == two):
time.sleep(2)
webbrowser.open("https://www.youtube.com/watch?v=QZbuj3RJcjI")
break
elif (clock == three):
time.sleep(2)
webbrowser.open("https://www.youtube.com/watch?v=QZbuj3RJcjI")
break
else:
print "finished"
break

Answer

1- How do I let my program run continuously while checking the clock value ?

-- Reassign the clock variables value within the while loop, this will keep checking against the updated timestamp

2- How do I let the program alive to run for the second and third value ?

-- Don't break out of your loop. Continue running

import time
import webbrowser

print(" This started on " + time.strftime("%H:%M"))

#values i want to assign for breaks
times = ["17:15", "21:15", "23:00"]

#continous loop
while True:
    clock = time.strftime("%H:%M")

    if clock in times:
        webbrowser.open("https://www.youtube.com/watch?v=QZbuj3RJcjI")

    time.sleep(60)