
Mad Libs were originally invented in the 1950's, and have appeared in numerous forms over the years (see wikipedia entry).
For example, suppose the tag-a-story is as follows.
With this tag-a-story we might get any of the substitution stories below:
The lab in Compsci 101 is terrible and I love it. The lab in Compsci 101 is long and I crave it. The lab in Compsci 101 is squeamish and I despise it.You'll be using one tag-a-story to develop and debug your program, then you'll be able to use any of the stories submitted by students in generating new stories. Note that words/phrases that will be replaced are delimited by angle-brackets: < and > as shown. We call these delimited words/phrases tags in this lab.
Start out by snarfing the code for this lab, or looking at the code directory.
There are six conceptual parts to this lab. Five correspond to a function you write in Madlib101.py and one is the creation of a story you upload.
-
Complete template_to_string.
This functiono reads the tag-a-story template file and creates one string from the file (which may consist of several lines).The code you get loops over the file and prints each line. Instead you should concatenate each line building one string you return.
- Complete read_tags.
Read a line of tags and phrases for each tag, in the format shown in code/tags.txt:adjective: awesome, boring, stupid, amazing, fantastic, confusing,silly, big, yellow animal: dog, cat, human, lion, pony, squirrel
The tag is the phrase to the left of the colon, and possible subsitutions are to the right of the colon, separated by commas. You should store the tag as the key in global dictionarytagstore
, where the value is a list of substitutes for the tag.In the code you're given the tags and substitutes are printed. Change the code so that the key is used to associate a list of substitute phrases. Note that it's possible that the same tag appears multiple times in the file of tags, e.g., you may see the tag adjective more than once. Be sure to .strip() individual words stored in the list of words associated with each tag.
- Complete create_story.
After completing the first two functions above this function should work by prompting the user for a response to each tag. Run the program to make sure things work. You may want to uncomment/comment theprint
statements to help debug your code.Replace
user
withauto
and see what happens when you run the program more than once. Leavecreate_story
in auto-generating mode. - Create and complete print_out.
def print_out(story,line_width)
This function should print all the words in the story (the first parameter, a string of words) using the specified line width (an int, the second parameter).Loop over the individual words in
story
(split) and print each one on the same line (usingprint w,
with a comma). Each time a word is printed update a count (int) with the number of characters printed. When this count is greater thanline_width
callprint
to goto the next line and reset the current count of the number of letters printed. You may want to print a space explicitly, don't forget to count this as one of the characters printed. - Be Creative.
You should write your own tag-a-story/Mad Lib and upload it to this site: http://www.cs.duke.edu/csed/tag-a-story/upload/. Pick a category and upload your file. You can verify that the file was uploaded by running the programURLreader.py
and choose the file you uploaded. Or you can visit this website that stores the uploaded files. - (time permitting)
Allow the user to choose a tag-a-story template from the web -- do this
by combining the code and functionality in
URLreader.py
with theMadLibs101.py
module to generate a story from a URL.