Take a Break
This program will remind an employee to take a break every two hours.
In plain English this is the logic for this program:
1. Wait 2 hours.
2. Open browser as a break reminder.
3. Create loop to repeat steps during the work day.
I will be using Python 2.7.14 to build this program. Once I open Python Idle, I create a new file to write my program on.
webbrowser.open() will be the function used to open the web browser. Within the () a Youtube link will be placed that will open summoned to open in a browser. Before I run this program, I also need to explicitly state the library which the function belongs to:
import webbrowser
webbrowser.open("https://www.youtube.com/watch?v=WSUFzC6_fp8")
I save this as breaktime.py and when I click run or F5, the program opens the browser to the defined Youtube link.
Next, I will call the sleep function from the time library. I will place 10 for seconds in the input to test that, it will wait 10 seconds before calling the open web browser function:
import time
import webbrowser
time.sleep(10)
webbrowser.open("https://www.youtube.com/watch?v=WSUFzC6_fp8")
When I save and run the program (F5), the program waits 10 seconds before opening the link on the function.
Now the program requires a loop to prompt multiple breaks during a time interval.
Below I've added the time.ctime to display the time at the start of each loop. The program is also written to go through this loop 3 times ever 10 seconds. Which means the Youtube link will be called every 10 seconds at three different intervals:
import time
import webbrowser
break_total = 3
break_count = 0
print("Program iteration time: "+time.ctime())
while(break_count < break_total):
time.sleep(10)
webbrowser.open("https://www.youtube.com/watch?v=WSUFzC6_fp8")
break_count = break_count + 1
Now in order to place this break reminder every two hours, I multiply number of seconds by minutes by two hours for the product equivalent. Note also that there are 3 total breaks which should be enough for the average 8 hour work day:
import time
import webbrowser
break_total = 3
break_count = 0
print("Program iteration time: "+time.ctime())
while(break_count < break_total):
time.sleep(60*60*2)
webbrowser.open("https://www.youtube.com/watch?v=WSUFzC6_fp8")
break_count = break_count + 1
Additional Concepts:
Abstraction hides how certain functions work. This happened earlier while referencing the time and webbrowser libraries to use respective functions, sleep and open.
No comments:
Post a Comment