Super Simple Python: Madlibs

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’ll be covering how to build a simple Madlibs-like game in under 15 lines of Python. 

We don’t need to import any libraries for this program, we’re just going to create a simple script. Much like the Inigo Montoya Introduction, we’ll be creating a set of texts to prompt the user. Unlike the introduction though, there will be no randomization. We’ll be taking string input and using it like we did in Rock, Paper, Scissors.

Creating Text Prompts

The first thing we need to do to create a Madlibs-like game is to create the text prompts. These are the sentences that are pre-created in Madlibs. We’ll create pieces of text that can have words attached to the end. For this example, we’ll be playing with the Humpty Dumpty nursery rhyme.

In a Madlibs game you are given a blank to fill in. For our example, we’ll create five text prompts.

text1 = "Humpty Dumpty sat on a"
text2 = "Humpty Dumpty had a great"
text3 = "All the king's men and all the king's"
text4 = "couldn't"
text5 = "Humpty Dumpty back"

Intaking Madlibs Input

Now that we’ve created the text prompts, we have to create a way for the user to “fill in the blanks”. We’ll simply use the Python input function which takes one parameter, a string to prompt the user with, and allows input from the user. In our case, we’ll use the texts we created earlier with an extra space for readability as the prompt. After each sentence, the user has a chance to complete the phrase.

word1 = input(text1+" ")
word2 = input(text2+" ")
word3 = input(text3+" ")
word4 = input(text4+" ")
word5 = input(text5+" ")

Showing the Final Construct

Once we have each of the five words or phrases that will finish each phrase, we can print out the final results to show the user their completed story. We’ll do this with a simple print statement. Notice that we don’t have any spaces at the end of each of the text prompts or the words. The print statement will add spaces by default when we separate the variables with commas.

print(text1, word1, text2, word2, text3, word3, text4, word4, text5, word5)

When we run our simple script, we should get an output like the one below. I answered with the words “doll”, “shawl”, “malls”, “ball”, and “on his doll”.

Python Madlibs Example with Humpty Dumpty

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: