Select Page

Find the right nanodegree program for you.

Start Learning

Do you know a friend who works way too many hours in front of a computer? Here’s a solution: write a Python program that encourages them to take a break after every two hours of computer work. How will we do it? We’ll create a Python program that will play their favorite song on YouTube after every two hours of work.

Here is what the output of our program will look like:

1. Install Python on Your Computer

Here are both the Windows and Mac instructions to get up and running with Python. (If you already have Python installed, go ahead and skip to step 3.)

How to install Python on Windows
How to install Python on a Mac

2. Open IDLE and Create a New File

Once you have Python installed, open IDLE. IDLE is the name of the place where you can write Python code. Open it on your computer.

On Windows: Go to Start -> Programs -> Python -> Idle
On a Mac: Type “Idle” in Spotlight

Create a new file by selecting: File -> New Window

3. Save and Run Your Program

The first thing we need to learn is to open a web browser with a YouTube URL. The following code does exactly that.

import webbrowser"")

Save the program; call it Now run the program: Run -> Run Module

The YouTube video should open. Try changing the URL to your favorite video.

4. Set Your Time for Your Video to Open

The next step is to set the amount of time before the video opens. Here’s how to make that happen.

import time
import webbrowser


Now run this. The program should now wait 10 seconds before launching the browser. Play around with the number of seconds. For example, 2 hours = 7,200 seconds.

5. Add a Loop to Your Reminder

As a final step, let’s add a loop to launch the browser multiple times during the day.

import time
import webbrowser

total_breaks = 3
break_count = 0

while(break_count < total_breaks):
    break_count = break_count + 1

That’s it! Show your program to a friend, and to learn more, take our Intro to Programming nanodegree program.