dark mode light mode Search Menu
Search

Python Mad Libs

Jenna on Flickr

“MadLibs” is a game where you write a story with missing words, then ask friends or family members to fill in the blanks. Since they don’t know what your story’s about, the result is often wonderful and silly!

Traditional MadLibs are done with paper and ink. But now that we’re in the digital age, we can recreate the game with some simple Python code, and reuse each MadLib over and over.

AN EXAMPLE MADLIB

To get you inspired, here’s a quick little story:

It was pizza day at school, and Jamie was super excited for lunch. But when she went outside to eat, a bird stole her pizza! Jamie chased the bird all over school. She climbed, jumped, and ran through the playground. Then she tripped on her shoelace and the bird escaped! Luckily, Jamie’s friends were willing to share their pizza with her.

On its own, the story isn’t super exciting. But let’s see what happens when we convert it into a MadLibs game:

It was ___(FOOD)___ day at school, and ___(NAME)___ was super ___(ADJECTIVE)___ for lunch. But when she went outside to eat, a ___(NOUN)___ stole her ___(FOOD)___! ___(NAME)___ chased the ___(NOUN)___ all over school. She ___(VERB1)___, ___(VERB2)___, and ___(VERB3)___ through the playground. Then she tripped on her ___(NOUN)___ and the ___(NOUN)___ escaped! Luckily, ___(NAME)___’s friends were willing to share their ___(FOOD)___ with her.

Now there’s a lot more potential for shenanigans!

GETTING STARTED

Open up your browser and navigate over to www.repl.it. Click the blue ‘+ new repl’ button in the top left corner.

Select ‘Python’ from the Language Menu and hit ‘create repl’.

Time to write your story! Make it as short or as long as you like. You can either write the story normally and then blank out keywords, or just jump straight to the MadLibs version. If you’re lacking inspiration, remember that cats, dinosaurs, space exploration, and magic portals always make good story subjects!

THE CODE

To make sure our program treats the story as text, and not code, we need to surround it with quotation marks. Then we can store it in a variable called ‘story’:

Next, we cut out all the ‘blank spaces’ and replace them with variables. Words that are repeated – like ‘Jamie’ or ‘pizza’ – use the same variable, but otherwise each variable name must be unique. Using descriptive names (like ‘noun1’, or ‘name2’, or ‘pizza’) will help you remember what each one represents. The variables are placed into the text like so:

"It was " + food + " day at school, and "

This is the trickiest part! You need to make sure that you have a ‘+’ sign before and behind each variable. You also need to make sure that each snippet of story starts and ends with quotation marks. If you’re missing a quotation mark, the colour of your text will be off, like so:

Instead of like so:

ESCAPING CHARACTERS

While simple, stringing together text and variables isn’t always easy! Here’s a ticklish detail: what if you have dialogue in your story, and you need to use quotation marks? If you just write the quotes normally, then you throw off the delicate balance between text and code.

The solution is to put a backslash (\) in front of each “text” quotation mark, to separate them from “code” quotation marks. We call this “escaping” a character. Don’t worry – the backslash won’t appear when you actually display the text.

name + " said \"Hi! My name is " + name + ".\""

PSST: a forward slash (/) is not quite the same as a backslash! (\)

PSST PSST: if this feels too confusing, you can use single quotes for your dialogue!

PROMPTING

In Python, we can use the “input” function to prompt a player for information. The text inside the function’s brackets is displayed in the console, where the user types their answer.

food = input("Enter a type of food: ")

To get all the necessary words for your MadLib, we need to add a similar line for every variable. In the code editor, these prompts go above the story section.

Finally, we end our program by displaying the finished story with the “print” function.

print(story)

FULL CODE LISTING

food = input("Enter a type of food: ")
jamie = input("Enter a girl's name: ")
adj1 = input("Enter an adjective: ")
bird = input("Enter a noun: ")
verb1 = input("Enter a verb in the past tense: ")
verb2 = input("Enter another verb in the past tense: ")
verb3 = input("Enter a third verb in the past tense: ")
noun1 = input("Enter a noun: ")
 
story = "It was " + food + " day at school, and " + jamie + " was super " + adj1 + " for lunch. But when she went outside to eat, a " + bird + " stole her " + food + "! " + jamie + " chased the " + bird + " all over school. She " + verb1 + ", " + verb2 + ", and " + verb3 + " through the playground. Then she tripped on her " + noun1 + " and the " + bird + " escaped! Luckily, " + jamie + "'s friends were willing to share their " + food + " with her."
 
print(story)

TIME TO PLAY!

Click the green ‘run’ button near the top of the screen. You should see your first prompt appear in the right-hand console. If so – great! Gather up friends and family, and have them answer each prompt until the story is finished and the delightful result is displayed onscreen.

If you get some sort of ‘SyntaxError’ message, double check that you’ve added all the necessary quotation marks and ‘+’ signs.

Another common error is misspelling variable names (NameError). This one’s a little trickier, because you won’t get an error message until you’re deep in the code:

If it feels like a lot to take in, start with simple sentences that have one or two variables. You can always work your way up to longer, wackier, and more complex stories once you’ve mastered the basics.

Have fun, and happy hacking!

Learn More

How to make your own MadLibs

https://hobbylark.com/party-games/How-to-Make-Your-Own-Mad-Libs

Python Escape Characters

https://cscircles.cemc.uwaterloo.ca/3-comments-literals/

Python Mad Libs

https://teachwithict.weebly.com/python-mad-libs.html

Build a simple mad lib

https://courses.p2pu.org/en/groups/playing-madlibs-with-python-building-your-first-game/content/build-a-simple-madlib-using-wing-ide/

Mad Libs Generator

https://www.projectlabyrinth.com/MadLibs/MadLibGen.php