To get some practice with dictionaries, in this assignment you’ll be writing a simple Mad Libs program in Python. The user will be able to enter a story as input, then run it for their friends. Using the program will look something like this:
Welcome to Mad Libs! Please type a story. I went to the {store}. It was {big} and {expensive}. While I was there, I bought a {humungous} {rat}. The {rat} became my {pet}. I love it very much. I hope to go to the {store} again one day. Please enter the part of speech of each of the following words: store: noun big: adjective expensive: adjective humungous: adjective rat: noun pet: noun Got it! Now, recruit a friend to play your story. Press enter to continue. ----------------MAD LIBS STORY----------------- Let’s create your story! Enter noun: bank Enter adjective: tasty Enter adjective: fluffy Enter adjective: green Enter noun: airplane Enter noun: family OK, let’s see what you wrote! I went to the bank. It was tasty and fluffy. While I was there, I bought a green airplane. The airplane became my family. I love it very much. I hope to go to the bank again one day.
Notice how this works. When entering the story, I can put certain words in braces. The program then prompts me to tell it the part of speech of all words in braces. If a word appears twice in braces, we assume it corresponds to only one blank in the game; it will always be filled in with the same value. Once we’ve gotten the parts of speech, we ask a new person to enter values for the parts of speech, and then replace the braces words with the actual words.
You’ll be using two dictionaries to create your program:
A dictionary that allows you to lookup words (like “big”) and get out parts of speech (like “adjective”).
A dictionary that allows you to lookup words (like “store”) and get out replacement words (like “bank”).
Let’s walk through, step by step. This cheat sheet might be helpful.
Write a function extract_braced_words that takes as its argument a “story string” in which certain words are surrounded by braces. It should return a list of all the braces-surrounded words. For example, if the input is the story from above, the output of this function should be ["store", "big", "expensive", "humungous", "rat", "pet"].
The best way to do this is as follows:
Use the find method on the story string to find the first { and the first }. (For example, story.find('{').) Remember that find returns the index at which a character occurs, or -1 if the character doesn’t occur.
Slice the string (you can slice strings, not just lists) using the indices found above, to get out the first braces-surrounded word.
Set story to story[indexOfRightBrace+1:] to chop off everything up to and including the first braced word.
Repeat all these steps, adding new words to a list, until you can’t find any more braces.
Write a function get_parts_of_speech which takes as its argument a list of words, and asks the user to enter the part of speech for each of them. It returns a dictionary that allows you to look up a part of speech for a certain word.
So, if I call get_parts_of_speech(["big", "apple", "walk"]), I’ll see the following on my screen:
Please enter part of speech for each of the following words. big: adjective apple: noun walk: verb
and the function should return {"big": "adjective", "apple": "noun", "walk": "verb"}, so that if the return value is called parts_of_speech, I can run parts_of_speech["apple"] and get back "noun", for instance.
To do this, create an empty dictionary, {}, and add to it as you loop through the list of words, asking the user to enter parts of speech.
Write a function get_word_replacements that takes as its argument a dictionary of words to parts of speech. This function creates a new dictionary, mapping words to replacements suggested by the “player” of the Mad Libs game.
The general structure is this:
Create an empty dictionary of replacements.
Loop through the words in the part-of-speech dictionary, using:
| 1 | for word in parts_of_speech: | 
Inside the loop, ask the user for a replacement word, by prompting them to enter a word of the correct part of speech. For example, if the word is “big”, then parts_of_speech[word] will be "adjective", and you should ask the user to Enter adjective:.
When the user enters a replacement, add it to the replacements dictionary, so that replacements["big"] is the new word for “big”, for example, “tasty”.
Once the loop is done, return the replacements.
Write a function do_replacements that takes two arguments: a story string (with braces-surrounded words), and a dictionary of replacements to apply.
Loop through the replacements, and for each one, change the story string so that every occurrence of word surrounded in braces is replaced with replacements[word].
Remember that there is a replace method for strings, which takes in an old and new string, and replaces all copies of old with new.
Outside of any function, below your other function definitions, write the main program. It should:
Print out “Welcome to Mad Libs.”
Print out “Enter a story.”
Ask the user to input a story, and store the story in a variable.
Call extract_braced_words on the story, to get a list of words_to_replace.
Call get_parts_of_speech on the list of words_to_replace, to get a dictionary of parts_of_speech.
Print "---------------- MAD LIBS STORY -----------------"
Print "Let’s create your story!"
Call get_word_replacements to get a dictionary of word_replacements.
Call do_replacements on the story and the word_replacements, to get a new version of the story.
Print "OK, let’s see what you wrote!"
Print the new story.