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.