Couplet Creator

"The which if you with patient ears attend,
What here shall miss, our toil shall strive to mend."

"To know our farther pleasure in this case,
To old Freetown, our common judgement place."

"Love is a smoke made with the fume of sighs;
Being purged, a fire sparkling in lovers' eyes..."

What do all of the above have in common? They’re couplets! A couplet is a literary device used in poetry. It consists of two lines, and the last words of the lines rhyme. Couplets can be found in poetry or, in a play, as part of a character’s speech. William Shakespeare is one of the most famous writers to use couplets. All of the examples above are from Shakespeare’s play Romeo and Juliet.

We’re going to use a couplet from Romeo and Juliet, plus some other words, to make a Couplet Creator using Python, a programming language. The couplet we are using is at the end of the Prologue of Romeo and Juliet. Here is the couplet: “The which if you with patient ears attend / What here shall miss, our toil shall strive to mend.” Basically, in this line the Chorus (the person/group reciting the Prologue) is telling the play’s audience that if the audience is patient, the actors will do their best to tell the story.

Here’s how the program works:

The program will ask the user for the first line of a couplet. The user will need to enter the first line of the couplet found at the end of the Prologue of Romeo and Juliet, minus the last word. At this point the user will type “The which if you with patient ears”. Next, after hitting the Enter key, the user will type the second line of the couplet, minus the last word. So, at this point the user will type “What here shall miss, our toil shall strive to”. Once the user hits Enter, the program will randomly assign a word from a list we gave it to the end of each line, giving the user a new version of the couplet from the Prologue.

Let’s get started!

  1. Step 1

    First, we need to tell our program that we want to use Python’s random module. This module will allow the program to randomly choose from a list of words to add to the end of our couplet. By importing the random module at the beginning of the program, we allow the program to randomly assign a word to the end of each line in the couplet later in the program.

    On line 2 of the code editor on the right, give the program a direct command to import the random module.

    Next we’ll learn about variables and values!

  2. Step 2

    Variables and Values

    In Python, we have variables and values. Think of a variable as a box, and a value as something that goes inside the box. Here is an example of a variable and its value:

    animal = “cat”

    In this case, animal is the variable, and “cat” is its value. In fact, it is a specific type of value--a string. A value that has letters or characters and no numbers is a string type value. We put strings inside quotation marks. When we write a variable, we are declaring the variable.

    In this case, we can say that we are assigning the value of “cat” to the variable animal. The = is called the assignment operator. It’s not an “equals” sign in the sense that 2 = 2, but it does convey a relationship. We are basically saying that animal represents “cat”. That means that when we do something with animal, we are doing something with “cat”.

    For example, look at this code:
    print(animal)

    The print function tells the program to print, or display, something in the output area of our code editor. A function is an action that we can do in Python. Keeping in mind that animal represents “cat”, what do you think we’ll see if we print the value of animal?

    Try it yourself: In the code editor on the right, declare a variable called animal. Then assign it a value, like “cat” (or another animal, if you’re not a cat person). Then tell the program to print the variable. Click the large gray triangle button at the top of the editor to run your program and see the results!

    You can also have multiple words as part of a single value. For example:

    favorite_song = “Somewhere Over the Rainbow”

    Try typing the above code in the editor and then printing favorite_song.

    Note that variable names are lowercase. Use underscores when you need to include multiple words, as in favorite_song, cat_name, and so on.

    Sometimes we might want to have more than one value assigned to a variable. We might want to have a list of values, for instance. In Python, we can make a list, also called an array. For example:

    animal = [“cat”, “dog”, “bird”, “fish”]

    “Cat”, “dog”, “bird”, and “fish” are all values in the array. In this example, they are all single-word strings. But you can have multiple-word strings as values in an array, as well:

    favorite_songs = [“Somewhere Over the Rainbow”, “Happy Birthday”, “Imagine”]

    What do you think would be the result if you typed the above line of code in your code editor and then printed favorite_songs? Try it and see!

  3. Step 3

    Next, we are going to make two lists of words. The first list will be the possible end words for the first line of our couplet (including the original end word of the line), and the second list will be the possible end words for the second line of the couplet (including the original end word of the line).

    All of the words will rhyme. We want to make sure that whatever words the program chooses, we still have a legitimate couplet whose last words rhyme. However, we don’t want the program to choose the same word to attach to the end of each line, which is why we need two separate lists.

    Let’s call our first list line_1_rhyming_words. Type this variable name on line 4 of your code editor. Its values should be three words-- "attend" plus two words that rhyme with “attend”. Like this:

    line_1_rhyming_words = [“attend”, “ ”, “ ”]

    Let’s do something similar on line 6. Here, declare a variable called line_2_rhyming_words and assign it a list of three values-- “mend” plus two words that rhyme with “mend”. Like this:

    line_2_rhyming_words = [“mend”, “ ”, “ ”]

  4. Step 4

    Now we need to ask the user to tell us the first line of the couplet from the Prologue, minus the last word. We need to get some input from the user. To do this, we will create a new value:

    input(“Please type the first line of the couplet from the Prologue in Romeo and Juliet, minus the last word. ”)

    and assign it to a variable:
    line_1 =

    Notice that we have put an extra space after the period before the end quotation mark. That way when the user types their response, there will be a space between the prompt and the response.

    Now we need to do the same thing to get the second line of the couplet minus the last word. On line 10, declare a variable called line_2 and assign it the value of the user input when asked to type the second line of the couplet minus the last word.

    Now we have two new variables, line_1 and line_2. The value of line_1 is the input we asked the user to type: The first line of the couplet from the Prologue, minus the last word. The value of line_2 is the input we asked the user to type: The second line of the couplet from the Prologue, minus the last word.

  5. Step 5

    Let’s recap what we’ve done so far: We’ve imported Python’s random module to allow our program to randomly choose values from our lists of rhyming words to create a new version of the couplet. We’ve created two variables, line_1_rhyming_words and line_2_rhyming_words, each containing a list of words, all of which rhyme.

    Next, we created two more variables, line_1 and line_2, and we are asking the user to provide the lines (all but the last word in each line) from the Prologue couplet as the values for these variables.

    We have all of the pieces we need for our couplet. Now we just have to somehow get two words and add them to our user input lines so that we have two complete lines in our couplet.

    This is where the random module comes into play. This module has a function called .choice() that we can use. A function is an action we can do in Python. We can type random.choice(), put the name of a list in the parentheses, and then an item will randomly be chosen from that list.

    Let’s see how this works. On lines 12 and 13 of your code editor, type:

    word = random.choice(line_1_rhyming_words)
    print(word)

    In the output area of the code editor, you should see one of the words from line_1_rhyming_words. The program randomly chose a word from that list, assigned it as the value of the variable word, and displayed it!

    Delete the code on lines 12 and 13. Now that you’ve seen how random.choice() works, let’s use it in our Couplet Creator!

    We need to create two more variables. These variables are going to contain the new versions of our couplet lines. We will call these variables line_1_new and line_2_new. Declare these variables on lines 12 and 13. We’ll add their values soon.

    We know that we want to get the new version of each line of our couplet, but first we need to teach the program what the formula is for each line. We will use concatenation to add together all of the pieces of each line of the couplet. Concatenation means “adding together”. We do this by using the + symbol, just as you do when adding in math class.

    Before we write the code, let’s look at it as a formula:

    New Line 1 of couplet = the user input we got for line 1 + a random word from our first list of words
    New Line 2 of couplet = the user input we got for line 2 + a random word from our second list of words

    We have variables and values to represent all of these pieces of our formula--let’s put them together! Add this code to line 12, after the = symbol:

    line_1 + “ ” + random.choice(line_1_rhyming_words)

    On line 13, we want to do the same thing, except we want to concatenate line_2, a space, and a random word from line_2_rhyming_words. Type the code to do this.

    Now the new version of the first line of our couplet will “equal” the user input (represented by line_1) “plus” a random word from the first list of rhyming words. The new version of the second line of our couplet will “equal” the user input (represented by line_2) “plus” a random word from the second list of rhyming words.

    By adding a “ ”, we add an extra space between the user input and the randomly chosen word. That way the user input and the word are not smashed up against each other.

  6. Step 6

    Now that we have taught our program the formula for the lines of our couplet, it’s time to tell the program to “print”, or display, both lines so that we have a complete, new couplet.

    On lines 15 and 16, type:

    print(line_1_new)

    print(line_2_new)

    Then run your program to see it in action!

    Try running the program several times to see different couplets. You may even end up seeing the original couplet from the Prologue--it all depends on which words the program randomly chooses.

View Solution

Teaching Ideas

  • Key terms & concepts: couplet, Python, module, random module, value, variable, string, function, print function, array, input, function, concatenation
  • Do this project while studying sonnets, couplets, poetry, or Shakespeare.
  • Have students find other couplets to use in their project other than the one in the Prologue.
  • Have students write original sonnets, including couplets, and then do this coding project after writing their sonnets.
  • Have students write critical analysis short responses or essays about the role of a couplet in a specific sonnet from literature.

Teaching Standards

ELA Common Core Standards (U.S.)

Reading Standards for Literature, 6-12–Grades 9-10 Students, #4(Pg 38):

  • Determine the meaning of words and phrases as they are used in the text, including figurative and connotative meanings; analyze the cumulative impact of specific word choices on meaning and tone (e.g., how the language evokes a sense of time and place; how it sets a formal or informal tone).

Reading Standards for Literature 6-12–Grades 11-12 Students, #4(Pg 38):

  • Determine the meaning of words and phrases as they are used in the text, including figurative and connotative meanings; analyze the impact of specific word choices on meaning and tone, including words with multiple meanings or language that is particularly fresh, engaging, or beautiful. (Include Shakespeare as well as other authors.)

Reading Standards for Informational Text 6-12

  • Grade 6 students #4 (Pg 39): Determine the meaning of words and phrases as they are used in a text, including figurative, connotative, and technical meanings.
  • Grade 7 students #4 (Pg 39): Determine the meaning of words and phrases as they are used in a text, including figurative, connotative, and technical meanings; analyze the impact of a specific word choice on meaning and tone.
  • Grade 8 students #4 (Pg 39): Determine the meaning of words and phrases as they are used in a text, including figurative, connotative, and technical meanings; analyze the impact of specific word choices on meaning and tone, including analogies or allusions to other texts.

Python References

For help with learning Python, check out these resources:

  • Think Java and Python for Everybody, both available for free at trinket.io
  • Head First Learn to Code: A Learner’s Guide to Coding and Computational Thinking by Eric Freeman
  • Impractical Python Projects: Playful Programming Activities to Make You Smarter by Lee Vaughan
  • Mosh Hamedani’s YouTube tutorial “Python Tutorial - Python for Beginners [2020]”
  • W3Schools.com - Website with information about many programming languages, including Python

Sitemap