How to create a Mad Libs game in Python
Learn how to create a 'Mad Libs' game with this free python web tutorial.
New Lesson |
Introduction:
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
Example:
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
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
Example:
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
sIntroduction
In this tutorial, you will learn how to 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.
Examples
In this tutorial, you will learn how to 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.
Examples
Activity 1 – Creating your first Mad Lib game
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
Type in and run the following code:
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
Type in and run the following code:
noun = input("Enter a noun: ")
print("Rumble in the " + noun) |
Use the code window (above) to test your code.
Challenge:
Example:
- Create a Mad Lib that contains 2 or even 3 user inputs.
Example:
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.") |
Activity 2 – Mad Lib (Version 2)
In the next example example, you 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 you create your text file, let's first explore 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, try the following code:
In the next example example, you 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 you create your text file, let's first explore 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, try the following code:
string = "This is a string"
string = string.replace("is", "was") print(string) |
Use the code window (above) to test your code.
In the above example, the line ‘string = string.replace(“is”, “was”) looks for the word “is” in the sentence and replaces it with the word “was”. We will be using the .replace() method to replace the blank(s) in the Mad Libs with the word(s) input by the user.
Step 2. Creating the text file
Next, we need to create a text file for their Mad Libs, for the purpose of this example, we will call the file 'madlibs.txt'.
Challenge
Make up some stories and store each one on a different line – Type in the word ‘blank’ for the word you want to be replaced, for example:
Step 2. Creating the text file
Next, we need to create a text file for their Mad Libs, for the purpose of this example, we will call the file 'madlibs.txt'.
Challenge
Make up some stories and store each one on a different line – Type in the word ‘blank’ for the word you 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! |
Use the code window (above) to test your code.
Step 3. Writing the code
1. Enter the following code (make sure that you read the comments explaining what the code is doing):
1. Enter the following code (make sure that you 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
import random # 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 print(madlib) |
Challenge:
- modify your code so that it displays the randomly chosen Mad Lib first before prompting the user for their response.
Resources:
Mad Lib Solution
|
|
You may also be interested in:
Keywords:
Python web, python programming language, python programming, python coding.
Python web, python programming language, python programming, python coding.