Courses‎ > ‎Intro to CS - Full Year‎ > ‎Spring‎ > ‎Dyrland-Weaver‎ > ‎Work‎ > ‎

Work 34: 5/6

posted May 6, 2019, 11:06 AM by JonAlf Dyrland-Weaver   [ updated May 6, 2019, 11:39 AM ]
REMINDER: Re-submit cypher_0 and cypher_1. Regardless of whether you did these on time, you must submit them again!

Mad Libs!

Mad Libs are stories where certain words are left blank and then replaced by someone who doesn't know the story. Here is a one sentence example:

"When you're ________ in a ______________ in _________ and it's __________ make sure to bring your _______" 
                       verb             weather type         city                      holiday                                          noun
Could become:
"When you're eating in a tornado in Brooklyn and it's flag day make sure to bring your toilet"

You will write a python program to fill in mad libs. You will need the following:
  1. A file that contains a story, but instead of blanks there is a placeholder. Get this file to start:
  2. A dictionary with keys that are word types and values that are lists of words of the type.
    • Put at least 5 words in each list, feel free to put more than 5.
    • For our above example, we might have the following dictionary:
      •  { 'verb' : ["eating", "swimming", "bathing", "lost"],
      •    'weather' : ["heat wave", blizzard", "hurricane", "rain"],
      •    'city' : ["Phoenix", "Brooklyn", "Toledo", "Juarez"],
      •    'holiday' : ["rosh hashannah", "flag day", "eid", "easter"]
      •    'noun' : ["toothbrush", "towel", "cat", "toilet"] }
  3. A function that returns a random element from a dictionary formatted like the one above. It should take 2 arguments, the dictionary and the key associated with the list you want an element from.
Once you have all of those pieces in place, you can do the actual mad libs part. You can code this any way you'd like, including adding extra functions, lists or variables, but you should follow the following general steps:
  1. Read the story from the given file into a string.
  2. Replace each placeholder word in the string with a randomly selected word from the appropriate word list.
  3. Print out the result.
When you run the program, it should print out a completed mad lib based on your story and the word dictionary

submit your python file and story file as madlibs