Assignment: Write a Mad Libs Script
In this lesson, we're going to learn how to write a simple mad libs script in Python.
What is a mad libs script??
Mad libs are stories or sentences with blank spaces that can be filled in with different words to create a funny or interesting story. For example, here's a simple mad lib:
I went to the __________ to buy a __________.
To fill in the blanks, we can use words like "store" and "banana" to create a sentence like this:
I went to the store to buy a banana.
Assignment Overview
Now, let's see how we can write a Python script to create a mad libs story. First, we need to come up with a story or sentence with blank spaces that can be filled in with different words. For this example, we'll use the following story:
I was walking in the park and I saw a __________. It was so __________ that I __________.
Next, we need to ask the user to fill in the blanks with different words. We can do this using the input() function in Python. Write a script that creates output like the following:
As you can see, the user entered the words "dog", "fluffy", and "petted" to fill in the blanks in the story, and the script printed the completed mad lib to the screen.
Assignment Clues...
This is how we are writing the script to generate the output above:
- Assign the output of the input() function to three variables
- noun
- adjective
- verb
- Create a story variable and assign the value to include the story and three variables above
- Print the story
If you get stuck, you can view our code solution by clicking the Code solution arrow below:
Code solution
No comments yet. Add the first comment to start the discussion.