Saturday, November 28, 2020

Ned Batchelder: Mad Libs

When people ask what they should implement to practice programming, I often say, Mad Libs. It’s a game, so it might appeal to youthful minds, but it’s purely text-based, so it won’t be overwhelming to implement. It can start simple, for beginners, but get complicated if you are more advanced.

Mad Libs is a language game. One person, the reader, has a story with blanks in it. The other player(s) provide words to go in the blanks, but they don’t know the story. The result is usually funny.

For example, the story might be:

There was a tiny     (adjective)        (noun)     who was feeling very     (adjective)    . After     (verb)    ’ing, she felt     (adjective)    .

(An actual story would be longer, usually a full paragraph.) The reader will ask for the words, either in order, or randomized:

Give me an adjective.
“Purple”.
Give ma a noun.
“Barn”.
A verb.
“Jump”.
Another adjective.
“Lumpy”.
Another adjective.
“Perturbed”.

Then the reader presents the finished story:

There was a tiny perturbed barn who was feeling very purple. After jumping, she felt lumpy.

To do this in software, the program will be the reader, prompting the user for words, and then output the finished story. There are a few different ways you could structure it, of different complexities:

  • Hard-code the story in the code.
  • Represent the story in a data structure (maybe a list?) in the code.
  • Read the story from a data file, to make it easier to use different stories.
  • Provide a way to edit stories.
  • Make a Mad Libs web application.

Each of these has design choices to be made. How will you separate the text from the blanks? How will you indicate what kind of word goes in each blank? How complex a story structure will you allow?

There are other bells and whistles you can add along the way, for any of the stages of complexity:

  • Randomize the order the words are requested.
  • Have a way for a provided word to be re-used in the story for some coherence.
  • Stories that have random paths through them, so that it is not always the same story, giving more variety.
  • Smarter text manipulation, so that “a” or “an” would be used appropriately with a provided word.

If you are interested, you can read the details of how I approached it years ago with my son: Programming madlibs.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...