In my last post, I showed off a program I wrote using Python to make a Mad Libs game. If you have been deprived of the hilarity that is Mad Libs (i.e. if you don't know what it is), check out my previous post about it or this wikipedia entry. Or you can just keep on reading because you'll probably figure it out just by reading this post. :-)
I promised in my last post that I would show you how to create your own Mad Libs game using Python, so read on...
Step One: Create!
The first step is to create your own Mad Libs story. You can make up a story on your own or use song lyrics, a nursery rhyme, or a poem.
In your story, to go along with the Mad Libs format, be sure to include blank spaces for the missing words and decide what part of speech (noun, pronoun, adjective, verb, etc.) belongs in each blank. Here is an example using the nursery rhyme "Humpty Dumpty":
(Person's name) Dumpty (verb in the past tense) on a wall.
(Person's name) Dumpty had a/n (adjective) fall.
All the king's (plural animal) and all the (profession)'s men
Couldn't put (Person's name) together again.
For this example, we have five variables: a person's name, a verb in the past tense, an adjective, a plural animal, and a profession. Yes, I am using the math term "variable" here, but that's only because it'll make it easier to understand when we start working in Python.
Step Two: Trinket!
Now that you've created your story (If you haven't, then STOP NOW and go back and write a story, lazybutt!), it's time to start writing your program.
In order to write your program, you have to have a place to write it, right? You can download a platform for Python (which I don't know anything about, sorry) or you can create an account at trinket.io. So go do that now.
Now that you have your new trinket account, create a new trinket using python. I'm not going to tell you how to do that. You're smart; I'm sure you can figure it out.
The new trinket you create looks like this:
You will write your program on the left side of the screen, and then when you click the "Run" button at the top, the output of your program is shown on the right side.
Step Three: Variables!
To convert your story into a usable program, first you have to set up your fill-in-the-blank items for the user (the person running your program and responding to it). To do that, use this format:
variable_name = raw_input ("The content you want the user to respond to.")
The words written in purple are the words that you will change. The rest of it is Python formatting that you have to maintain. Here is an example I would use from my "Humpty Dumpty" story:
persons_name = raw_input ("Give the name of a person.")
There are a few things you need to know when you're setting up your questions. First, your variable name (the thing that comes before the equal sign) cannot be separated by a space, so if I tried using this--persons name--it wouldn't work. Instead of using a space, using an underscore if you absolutely have to have two words.
In addition, you have to give each of your variables a different name. Say for instance that in your story, you are asking for three different nouns. Each of those nouns needs a different variable name in order for your program to work smoothly. To make things easy, you can just call them noun1, noun2, and noun3. For instance:
noun1 = raw_input("Name a noun.")
noun2 = raw_input("Name another noun.")
noun3 = raw_input("Name yet another noun.")
One more thing: the punctuation you use has to exactly match the format I've shown you. If you forget to close your parentheses or forget the quotation marks, your program will not work properly. Make absolutely sure that your punctuation is correct.
Before we go on to the next part of your program, try running the program you have so far. If you've typed everything correctly, then the program will prompt you to answer each question. Go ahead. Answer each question and see what happens.
Step Three: Story!
After you answered each question, not much happened, did it? That's because we haven't put the answers into the story yet. Let's do that now.
The next thing you want to do is write your story and tell the program to grab the user's answers to your questions to replace the blanks in your story. Here is the format for that:
print "This is your story right here. %s The percent signs represent the blanks in your %s story. They are replaced by the variables within the %s parentheses that follow the stuff in quotation marks." % (variable_name1, variable_name2, variable_name3)
Again, the words in purple are the parts you will change based on the needs of your story. The items in black have to look exactly like that so that Python will understand what to do. So for my Humpty Dumpty story, this is what I would write:
print "%s Dumpty %s on a wall. %s Dumpty had a/n %s fall. All the king's %s and all the %s's men couldn't put %s together again." % (persons_name, verb, persons_name, adjective, animal, profession, persons_name)
Step Four: Addendum!
Note that I used the person's name multiple times, so I just repeated it several times in my variable list in the parentheses so that the program would know where to put it in the story.
Also, you may have noticed that your story kept going on and on and extending way across the page. If you want to prevent that from happening, you can tell Python that you want to continue your text on the next line. To do that, insert a backslash (\) and hit Enter on the keyboard wherever you want to break to the next line, like this:
print "This story is getting really long. I mean, it's getting so long that it is bleeding across the page. Arrgh! \
Okay, I put in a backslash so that it wouldn't be so long because, man, it was getting really long, wasn't \
One more thing--if your output on the right side of the screen is too squished together--in other words, if you want to put in blank lines--just add the word "print" in the places where you want a blank line.
For example, I write this:
noun = raw_input("Name a noun.")
adjective = raw_input("Name an adjective.")
verb = raw_input("Name a verb.")
And it looks like this in the output (the right-hand) screen:
Name a noun.
Name an adjective.
Name a verb.
It doesn't look like this:
Name a noun.
Name an adjective.
Name a verb.
Step Five: Share!
After you've finished and trouble-shooted (trouble-shot?) your work, show it to all your friends (and enemies), post it to Facebook, tweet about it, and share it everywhere. Also, please put a link to it in the comments, and I'll look at it as well. And finally, if you have any questions, I'll try to answer them. Just leave them in the comments below. Thanks!
And if you liked these instructions, please share them. Tweet or share this post on Facebook by clicking the buttons below. Thank you!!!!!!
Word and Book Lover.