Super Simple Python: Madlib

Super Simple Python is a series of Python projects you can do in under 15 minutes. In this episode, we’re going to make a Python Madlib in under 15 lines of code.

We don’t need to import any libraries for this program, we’re just going to create a simple Python Madlib 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 for Python Madlib

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 Python Madlib 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 Constructed Python Madlib

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 Madlib Example with Humpty Dumpty
Python Madlibs Example with Humpty Dumpty

Further Reading

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.

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
Yujian Tang

Leave a Reply

%d bloggers like this: