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:

Enter a noun: dog
Enter an adjective: fluffy
Enter a verb: petted

I was walking in the park and I saw a dog. It was so fluffy that I petted it.

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

To summarize, mad libs are stories or sentences with blank spaces that can be filled in with different words to create a funny or interesting story. In Python, we can create a mad libs script by asking the user to fill in the blanks, using their input to create the story, and then printing the completed story to the screen.

I hope you enjoyed this assignment and I am looking forward to seeing you in the next lesson!

Server Academy Members Only

Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments

Saving Progress...

Sign up for free!

Sign up for free and get instant access to this course!.

Python 3 Fundamentals

0%

0/1 Lessons

Installing Python on Windows

• 1hr 17min

0 / 4 lessons complete

Python Basics

• 28min

0 / 7 lessons complete

Python Variables

• 41min

0 / 8 lessons complete

Even more Python Variables!

• 41min

0 / 6 lessons complete

Conditional Statements

• 15min

0 / 3 lessons complete

Writing Functions

• 30min

0 / 5 lessons complete

Python Loops

• 23min

0 / 5 lessons complete

Python PIP and Modules

• 18min

0 / 4 lessons complete

RegEx

• 26min

0 / 4 lessons complete

Working with APIs

• 12min

0 / 3 lessons complete

Course Conclusion

• 2min

0 / 1 lessons complete