CSC 226

CSC 226 logo

Software Design and Implementation


Strings and Mad Libs

Objectives

  • 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 Parade presents more examples than you might imagine of what people can do as a hobby.

The palindrome.py 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 http://www.madglibs.com/.

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)-p7.py 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. 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.

  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 palindrome.py to see some string functions being used.
  • Include comments for any parts of the code that is non-intuitive to let us 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.
When you are finished writing and testing your assignment, drop your source code, YourUserName(s)-p7.py into the p7 dropbox on Moodle.

Copyright © 2014 | http://cs.berea.edu/courses/csc226/