How to create a Mad Libs game in Python - Part 1
Mad Libs tutorial
- Understand and use sequence in an algorithm
- Understand and use iteration in an algorithm (FOR and WHILE loops)
- Understand and use data structures in an algorithm (for example, Lists, Tables or Arrays)
EDUCATOR: COMPUTATIONAL THINKING COMPETENCIES:
COMPUTER SCIENCE EDUCATORS:
CSTA K–12 CS Standards:
AREA OF LEARNING AND EXPERIENCE: Science and Technology:
Computation is the foundation for our digital world.
Progression step 3
- I can use conditional statements to add control and decision-making to algorithms.
- I can identify repeating patterns and use loops to make my algorithms more concise.
- I can explain and debug algorithms.
Progression step 4
- I can decompose given problems and select appropriate constructs to express solutions in a variety of environments.
- I can select and use data structures that efficiently manage data in algorithms.
- I can plan and implement test strategies to identify errors in programs.
Progression step 5
- I can identify, define and decompose problems, choose appropriate constructs and express solutions in a variety of environments.
- I can use file-handling techniques to manipulate data in algorithms.
- I can test, evaluate and improve a solution in software.
What is a Mad Libs
According to Wikipedia, Mad Libs is a phrasal template word game, usually played at parties, where one player prompts other players for a list of words to substitute blanks in a story before reading out the completed story aloud. Source: https://en.wikipedia.org/wiki/Mad_Libs
We start with a story, containing several blanked out words. Here’s a basic example:
Rumble in the _______.
We then prompt the user to fill in the gaps, in this case, with a noun or place. We might end up with something like this:
Rumble in the Toilet.
Obviously, the more entertaining the original story, the better the laughs will be at the end!
To find out more, visit the Mad Libs wiki page: en.wikipedia.org/wiki/Mad_Libs
In this lesson, students will code a “Mad Lib” game in Python. The game will work by prompting the user to enter some words (e.g. person’s name, noun, adjective, place, object etc.) and substitute these with blanks in a story.
Direct students to one of the online Mad Lib games listed below. Ask students to submit random words (as instructed) and note down some of the answers. Ask students to note down any unusual answers.
Note: Use these sites at your own risk. teachwithict.com is not responsible for the content of external Internet sites.
Online Mad Lib games:
- Wacky Web Tales – https://www.eduplace.com/tales/
- Mad Libs - http://www.madglibs.com/ (Click on each tile to reveal a mad lib).
Print out some example mab libs from madlibs.com and give to students to complete in small groups - http://www.madlibs.com/printables/ Tip: I find that the Mad Libs ‘Hall passes’ are always a hit: http://www.madlibs.com/content/uploads/2016/04/KIDS-excuse-hall-pass_1_.pdf
Display the mad libs starter PowerPoint (below) on the board and ask students to suggest words to fill in the blanks. You could get the class to vote for their favourite suggestions.
Once students have completed the starter activity, inform them that they are going to create a variation of the Mad Libs format using Python.
There are several ways to create a Mad Lib game in Python. In our version, the program will pick a story at random from a list however, before we start coding our “Ultimate Mad Lib” game, let’s start with a simple version:
Step 1. Creating your first Mad Lib
Instruct students to type in and run the following code:
noun = input("Enter a noun: ")
print("Rumble in the " + noun)
Challenge students to:
- comment their code, using the hashtag (#), explaining what the code is doing.
- create their own mad lib (If students struggle to come up with their own Mad Libs, they can use the examples from the starter activity).
- create a Mad Lib that contains 2 or even 3 user inputs.
noun = input("Enter an animal: ")
noun2 = input("Enter another animal: ")
noun3 = input("Enter an object: ")
print("The " + noun + " and the " + noun2 + " went to sea in a beautiful pea green noun3.")
In the next example students are going to create a text file containing several mad libs. Just like the previous example, the program will ask the user to enter one or several nouns, verbs, adjectives etc. but this time will pick a Mad Lib at random from a text file.
Step 1. Introducing the ‘String replace method’
Before students create their text file, introduce them to the ‘string replace method’.
Sometimes in Python, you find yourself in a situation where you want to modify the contents of string by replacing one piece of text with another. Luckily, Python makes this job easy thanks to the string replace method.
1. To understand how the .replace() method works, ask students to try the following code:
string = "This is a string"
string = string.replace("is", "was")
Explain to students that the line ‘string = string.replace(“is”, “was”) looks for the word “is” in the sentence and replaces it with the word “was”. Tell students that they will use the .replace() method to replace the blank(s) in their Mad Libs with the word(s) input by the user.
Step 2. Creating the text file
Next, the students need to create a text file for their Mad Libs– suggest saving the file madlibs.txt. Challenge students to make up some stories and store each one on a different line – instruct students to type in the word ‘blank’ for the word they want to be replaced, for example:
It don't mean a thing if it ain't got that blank!
Rumble in the blank!
I can't start my day without my blank!
1. Instruct students to copy the following code (making sure that they read the comments explaining what the code is doing):
# Program to read a random Mad Lib from a file and print the Mad Lib with the user's response
# Open the Mad Libs text file
f = open('madlibs.txt','r')
# Read the whole file and store each line in a list
madlibText = f.readlines()
# Choose a random line from the list
madlib = random.choice(madlibText)
# Ask the user to input a noun
noun = input("Enter a noun: ")
# Replace the blank with the user's input
madlib = madlib.replace("blank", noun)
# Print out the Mad Lib including the user's response
Challenge students to:
- modify their code so that it displays the randomly chosen Mad Lib first before prompting the user for their response.