Mad libs

Assignment overview

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.

Implementation

You’ll be using two dictionaries to create your program:

  1. A dictionary that allows you to lookup words (like “big”) and get out parts of speech (like “adjective”).

  2. 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.

1Get a list of words in braces {…}

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:

2Ask for parts of speech

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.

3Ask for replacement words

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:

4Do 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.

5Put it all together

Outside of any function, below your other function definitions, write the main program. It should: