Mad Libs is a phrasal template word game which consists of one player prompting others for a list of words to substitute for blanks in a story before reading aloud.
The game is frequently played as a party game or as a pastime.
Creating a Mad Libs game is simple. First we need to make a story, then we will remove some words from it and ask the other player to give alternate words. Then, we will read the story with the words the other player supplied.
The story becomes funny depending on the words the other player gave.
We will write a Mad Libs game in Python. The sample story we will use is as follows:
Roses are <color-here>.
<plural-noun> are blue.
I love <celebrity-name>.
We will ask the player the 3 blanks and based on their input we will print the story.
Save the below program in a file named 'mad_libs.py'.
color = input("Enter a color: ")
pluralNoun = input("Enter a plural noun: ")
celebrity = input("Enter a celebrity name: ")
print()
print("The story is:")
print()
print("Roses are {}.".format(color)) # Purple
print("{} are blue.".format(pluralNoun)) # Gates
print("I love {}.".format(celebrity)) # Keanu Reaves
First, we have taken the 3 blanks as inputs from the player and then we have printed the whole story after filling in the blanks with the player's input.
Run the program by entering the following command in Terminal/Command Line.
python3 mad_libs.py
A sample output will be as follows:
Enter a color: Purple Enter a plural noun: Gates Enter a celebrity name: Keanu Reaves The story is: Roses are Purple. Gates are blue. I love Keanu Reaves.
Comments