CSC 226

CSC 226 logo

Software Design and Implementation

T8: Mad Libs and Formatting strings

Assignment T8 should be completed in a pair programming partnership, generally two students.


  • Practice more breaking a larger problem down into smaller "pieces" using functions
  • Gain practice using strings

Palindrome Code that uses strings

A palindrome is a word, line, verse, number, or a sentence that reads the same backward as forward when you ignore punctuation and spacing. Examples include "Madam, I'm Adam" or in "Poor Dan is in a droop". Some people love making palindromes, and Palindrome List Palindrome Parade/a> presents more examples than you might imagine of what people can do as a hobby.

The code is an example of a program in Python that uses several of the features of the string class to check for palindromes. It might be useful as a starting point for this assignment.

Assignment Specifics

This assignment is to be designed together in pairs in class.

We are going to write a program to generate Mad Libs using a process called mail-merging which can actually be useful when you are sending out your resume to find a job. :)

What is a Mad Lib?
A Mad Lib is a game in which words are substituted for blanks in a story. Each blank is specified by a generic category like "noun", "verb", "adjective", "place", etc. The words are chosen by the category and substituted into blanks in the story. This process often produces funny results. You can try some Mad Libs at

Your primary objective is a design problem. You must figure out how to design a program which will generate a Mad Lib story when run by the user.

Create a file called YourUserName(s) containing your program which should do the following:

  1. You may include your story as a triple-quoted string, which will allow it to span multiple lines.

  2. This story string will contain the unchanging parts of the story (the template) as well as the blanks that will be replaced by words from categories input by the user. Your story string may say whatever you want it to say, but it must have at least 5 merge-fields which will be delineated by place holders ({1}, {2}, ... {n}). At least one of these fields must appear in the story more than once, which works just as you might hope using the Python string format method.
    For example, your story string might look like something like:
    '''Be kind to your {0}-footed {1},
    For a {2} may be somebody's mother.
    Be kind to your {1} in the {3},
    Where the weather is always {4}.'''
  3. When the program runs, it should ask the user to input various words for the categories that will replace the blanks in the template. The program can prompt for that information with questions or commands like:
    1. Enter your choice of noun:
    2. Enter your choice of noun:
    3. Enter your choice of noun:
    4. Enter your choice of place:
    5. Enter your choice of adjective:

  4. After the user has entered all of the words, the program should then display the completed story on the screen. For example, suppose the user entered:
    1. Enter your choice of noun: dog
    2. Enter your choice of noun: table
    3. Enter your choice of noun: lamp
    4. Enter your choice of place: phone booth
    5. Enter your choice of adjective: tall

    The completed story becomes:

    Be kind to your dog-footed table,
    For a lamp may be somebody's mother.
    Be kind to your table in the phone booth,
    Where the weather is always tall.

  5. Hint: The various words that are input from the user for each category might best be stored in a list of strings, so you can have access to them. For example, once the data is input by the user, the list in the example above will look like:
    ["dog", "table" "lamp", "phone booth", "tall"]
    Think about how once could get this list constructed from data entered by the user.

  6. You may design your code however makes sense to you, but realize that there are much easier ways and much harder ways to design this program, so please design before you implement!!

Be sure to also note the following:

  • Look carefully at to see some string functions being used.
  • Include comments for any parts of the code that is non-intuitive to let the reader know what that portion does.
  • Make sure that each function has parameters that make sense. If a function does not need a parameter, do not include one.
  • Use meaningful variable names.
  • Include a descriptive header as a comment at the top of your source code.
  • Includes a main() function.
  • The highest level of your program (i.e., no indenting) should only contain the following:
    • the header
    • any import statements
    • function definitions
    • a call to the main() function
  • Use functions for encapsulation with triple-quoted docstring for each of these functions. This docstring must include a description of the main purpose of the function, and descriptions of all input parameters as well as what is returned by the function.
  • Remember that you only need to submit a single program per team and that you must include both team members' names and the roles you had in each of the python program files.  It is also a good idea for the partner who is not submitting to upload the name of his or her partner.

Copyright 2016 | | Licensed under a Creative Commons Attribution-Share Alike 3.0 United States License