A note on resources: This tutorial was created on Mac OS X Version 10.9.5. I used the default pre-installed version of Python, version 2.7.5. I used Apple’s pre-installed Terminal application and Bare Bones Software’s free TextWrangler download.

  1. Open TextWrangler
  2. Enter # -*- coding: utf-8 -*- to allow for unicode characters. I had trouble with question marks and semicolons causing errors when running my code; this discussion seemed to fix the problem.
  3. Determine which parts of speech you will be replacing in your text for the Mad Lib. Start simple; for this tutorial, I replaced singular nouns, present tense verbs, adjectives, and adverbs.
  4. Define the part of speech
    1. Type the part of speech label
    2. Type = to indicate the words it will representScreen Shot 2015-04-13 at 3.06.55 PM
    3. Type your values between brackets [ ]Screen Shot 2015-04-13 at 3.07.16 PM
    4. Each value should be enclosed by quotation marks ” “Screen Shot 2015-04-13 at 3.07.27 PM
    5. Separate each value by a comma outside of the quotation marks “,Screen Shot 2015-04-13 at 3.07.37 PM
    6. Repeat for each part of speech you are defining; each list should be on a different line (example below)Screen Shot 2015-04-13 at 4.11.40 PM
  5. Type import random on a new line to import the module that will allow for you to randomize your textScreen Shot 2015-04-13 at 3.07.58 PM
  6. Paste your text on a new line and format. You can use any text, or create your own!
    1. Type print in front of your textScreen Shot 2015-04-13 at 3.09.57 PM
    2. Ensure the text is between quotation marks – this will ensure it appears as-is when the code is runScreen Shot 2015-04-13 at 3.11.04 PM
    3. Consider separating your text into multiple lines, if it is not already, in order to make the code readable
    4. Edit your text to suit the code. Ensure that \ are properly added to words with apostrophes or quotation marks that you would like to print when the code is run. Ensure all apostrophes/quotations are opened and closed properly. Ensure each line has “print” in front of it.Screen Shot 2015-04-13 at 3.12.49 PM
    5. Replace the words with the name of the part of speech in all caps and exclude them from the parentheses enclosing the string. This is so you can easily identify where you will paste the code for the random text generator.Screen Shot 2015-04-13 at 3.26.25 PM
    6. You can take this opportunity to paste in the random.choice code from step 7a, however I suggest waiting so that you can easily review the words that you removed.
    7. Ensure that the words you are replacing match the tense; for example, do not replace a plural noun if the words in your noun list are singular.
  7. Enter the randomization code into your text
    1. Replace the parts of speech in the text with random.choice(part of speech)
    2. Separate the random.choice code from the string of text with a comma
    3. Enclose the word before and and the word after the random.choice code with a quotation mark.Screen Shot 2015-04-13 at 3.34.21 PM
    4. Here is an example of the final code:Screen Shot 2015-04-13 at 3.34.09 PM
  8. Run your code through Terminal by opening the directory where your TextWrangler file is saved typing python filename.pyScreen Shot 2015-04-13 at 3.36.49 PM

Here is what the code looks like in its entirety:

Screen Shot 2015-04-13 at 3.40.35 PM

Here are a few examples of the results:

Screen Shot 2015-04-13 at 2.17.53 PM Screen Shot 2015-04-13 at 4.28.36 PM Screen Shot 2015-04-13 at 4.28.53 PM

Advertisements

One thought on “ENGL 539 Python Tutorial: Create Your Own Mad Libs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s