Python: MadLibs

Comments Off on Python: MadLibs

Learning parts of speech is a lot more fun when we can combine it with MadLib style learning. In this tutorial, you’ll learn how to create your own MadLibs for others to fill out.

Getting Started with Lists

Our very first step is just to create a hard-coded MadLibs sentence. We get the words from our player and then put them into our sentence. Try it out to see how it works. You can re-run the program by clicking on the triangular play button.

Here, we’ve collected four words and stored them in four different variables.  It would be easier to manage if they were all stored in a single list variable. At first, our lists will seem like more work, but in the end, you’ll see that they allow us to add more MadLibs with much more ease.

First we need to initialize our list. So place responses = [] on the first line of the code. The [] indicate that we are creating an ordered list.

Next, we’re going to update our variable assignments to use our list. So adjective1 = input('Please provide an adjective: ') becomes responses.append(input('Please provide an adjective: ')). Be sure to not miss the final ‘)’. This adds the input to the end of our list. Do this for each input.

Finally, we need to update our sentence. Since we’ve used a list, we now just need to point to the list and it will read off the list items in order. Replace the format(adjective1, animal, adjective2, noun) with format(*responses)

Be sure to test out your code with the original words to make sure everything lines up the way you expect. It’s always a good practice to try breaking your code, too. What happens if you answer with numbers or non-alpha-numeric characters?

Making our Lists do more work

So far our lists don’t seem very helpful, but we’ll start to change that now. It is the truth about programming that often things get worse before they get better.

First, notice how repetitive our inputs are. For four lines in a row, the only thing that changes is the part of speech. It’s not terrible for four lines, but imagine a much longer MadLib and you can see how it would get tedious. Especially if you decided you wanted to change that input.

Instead, we’ll create a list with just our parts of speech. At the start of our code, we’ll create a new list named ‘words’. Since the entries are hard-coded, we can add them when we initialize the list: words = ["adjective", "animal", "adjective", "noun"]

Now we’ll replace all of our responses.append() statements with a loop. The loop will cycle through our words and collect inputs for each of them.

for word in words:
  responses.append(input("Please enter a[n] " + word + ": "))

Fully automating our Lists

If we’re going to create a library of MadLibs, instead of just one, then we need to be able to pair our sentence template with the words we want to fill it with in a way that our program can recognize.

The easiest way to make sure our sentence and words stay together is to place them both in another type of variable called a dictionary. A dictionary is similar to a list, but instead of requiring the elements to be in order, we give them each a name (called a key) and reference them using it. Place:

fox = {}
fox['words'] = ["adjective", "animal", "adjective", "noun"]
fox['sentence'] = "The {} brown {} jumped over the {} {}."

at the beginning of your code and update all references to ‘words’ and ‘sentence’.

Mixing it up

The whole point of our lists was to make it simple to add more MadLibs to our program. We’ll add another one to the program, a list to store them each in, and a way to randomly choose one to use.

Add import random at the beginning of the file as well as an empty list named “madlibs”. Also, add the following new MadLib:

roses = {}
roses['words'] = ["plural noun", "color", "plural noun", \
  "color", "noun", "adjective"]
roses['sentence'] = "{} are {}, {} are {}.\n{} is {}, and so are you."

We’ll need to append the fox and roses madlibs to the madlibs list. Try that on your own, now. After that, we’ll need to use random to choose an item from that list. Insert madlib = random.choice(madlibs) before creating the empty ‘responses’ list. The variable ‘madlib’ is now equivalent to either ‘roses’ or ‘fox’. Update later references to ‘fox’ to ‘madlib’ so that we can use the random choice.

Ideas for expansion

Now that we have completed our program, you should definitely add several more MadLibs to make it more interesting. You could also consider cleaning up a number of details, such as replacing “a[n]” with the proper article based on the first letter of your word and capitalizing the first letters of sentences.