DEV Community

Cover image for Building a Mad Libs Word Game in Python
Scofield Idehen
Scofield Idehen

Posted on • Originally published at blog.learnhub.africa

Building a Mad Libs Word Game in Python

Mad Libs is a classic word game where one player prompts others for various words like nouns, verbs, adjectives, etc. without providing any context. The prompt words are then inserted into a story template full of blanks to create a hilarious or nonsensical story that the players read aloud for laughs.

Mad Libs has entertained generations of kids and adults since it was invented in the 1950s. Numerous television shows and games have also adapted the concept as an interactive storytelling format. Let’s learn how to code our text-based Mad Libs game in Python.

Python Loops: A Comprehensive Guide for Beginners

Learn about loops here: Python Loops: A Comprehensive Guide for Beginners

Step 1 – Setting up the game template

First, we must create our Mad Libs story template as a string in Python. This will contain the full story text with placeholders for where player input must be inserted, like {noun}, {verb}, etc.

For example:

story = “The other day my friend and I spotted a/an {adjective} looking {noun} on the sidewalk. My friend decided to {verb} it while I {verb}-ed.”
Enter fullscreen mode Exit fullscreen mode

Step 2 - Prompt for words from a player

We will use Python’s input() function to prompt the player to enter words for each placeholder in the story template string. Let’s store each input in separate variables like:

adjective = input(“Enter an adjective: “)
noun = input(“Enter a noun:”) 
verb1 = input(“Enter a verb: “)
verb2 = input(“Enter another verb: “)
Enter fullscreen mode Exit fullscreen mode

Step 3 – Insert words into the story template

Next, we insert the words stored in the variables into the story template using string concatenation to replace each {placeholder}.
For example:

story = “The other day my friend and I spotted a/an “ + adjective + “ looking “ + noun + “ on the sidewalk. My friend decided to “ + verb1 + “ it while I “ + verb2 + “-ed.”
Enter fullscreen mode Exit fullscreen mode

Step 4 - Print out the full story

Finally, print out the full Mad Libs story to display to the player!

print(story)
Enter fullscreen mode Exit fullscreen mode

At this point, we have built the basic functionality. Let’s look at some ways to expand the program next.

Adding Loops

Using loops, we can fill multiple blanks in the story without repeatedly prompting the player manually for each one.

For example:

    adjectives = []
    for i in range(3):
       adj = input("Enter an adjective: ")
       adjectives.append(adj)

    story = "...a/an " + adjectives[0] + "..." + adjectives[1] + "...extremely " + 
    adjectives[2] + ..." 
Enter fullscreen mode Exit fullscreen mode

Creating a GUI

Instead of a text interface, we can use the tkinter module to build a graphical user interface for a more interactive visual experience.

10 Jobs Requiring Little Formal Education

There are a lot of tech jobs you do not need coding skills, find out more in this guide: 10 Jobs Requiring Little Formal Education

We first import tkinter to allow us to create GUI elements.

    import tkinter as tk
Enter fullscreen mode Exit fullscreen mode

Create the main window

Using tkinter, we create a Tk() object, which will be the main window of our GUI.

    root = tk.Tk()
    root.title("Mad Libs")
Enter fullscreen mode Exit fullscreen mode

Adding input fields

We use the Entry widget to create input fields to collect words from the user.

    noun_entry = tk.Entry(root) 
    noun_entry.pack()
Enter fullscreen mode Exit fullscreen mode
# Repeat for other word types (verb, adjectives, etc)
Enter fullscreen mode Exit fullscreen mode

Adding a text display box

We use a Text widget to display the final mad lib story to the user.

    story_text = tk.Text(root, height=8, width=40)
    story_text.pack()
Enter fullscreen mode Exit fullscreen mode

Adding a Play Again button

We use a Button widget to create a clickable Play Again button to restart the game.

    play_button = tk.Button(root, text="Play Again", command=play_again) 
    play_button.pack()

    def play_again():
       # Reset game
Enter fullscreen mode Exit fullscreen mode
   ...
Enter fullscreen mode Exit fullscreen mode

Putting it together

We arrange all the GUI widgets using .grid() and .pack() for our layout.
We can now prompt the user for input, build the story, and display it in the GUI instead of just text printouts. The full code would connect the GUI elements to the game logic.

    import tkinter as tk

    # Main story template 
    story = "One day my friend and I found a {adjective} looking {noun} on the {noun}. My friend decided to {verb} it while I {verb}ed."  

    # Functions
    def play_again():
       # Clear entries
       adj_entry.delete(0, tk.END)
       noun_entry1.delete(0, tk.END)

       # Clear story  
       story_text.delete(1.0, tk.END)

    def generate_story():
       # Get values
       adjective = adj_entry.get()
       noun1 = noun_entry1.get()  
       noun2 = noun_entry2.get()
       verb1 = verb_entry1.get()
       verb2 = verb_entry2.get()

       # Create full story
       story_text.insert(tk.END, story.format(adjective=adjective, noun=noun1, noun2=noun2, verb=verb1, verb2=verb2))

    # GUI Setup
    root = tk.Tk() 
    root.title("Mad Libs")  

    # Input fields
    adj_entry = tk.Entry(root)
    adj_entry.pack()

    noun_entry1 = tk.Entry(root)  
    noun_entry1.pack()

    noun_entry2 = tk.Entry(root)
    noun_entry2.pack()

    verb_entry1 = tk.Entry(root)
    verb_entry1.pack()

    verb_entry2 = tk.Entry(root) 
    verb_entry2.pack()

    # Text display box 
    story_text = tk.Text(root, height=8, width=40)
    story_text.pack()

    # Play again button  
    play_button = tk.Button(root, text="Play Again", command=play_again)
    play_button.pack()

    # Run main loop
    root.mainloop()
Enter fullscreen mode Exit fullscreen mode

Let me know if you need any clarification or have additional questions!

Conclusion

Building games like Mad Libs is an engaging way to learn Python programming. The key concepts we applied here were:

  • String manipulation
  • Variables
  • User input
  • Print output
  • Functions
  • Logic

This combines core programming principles with creativity. These skills provide a foundation for developing more advanced games, apps, and Python tools.

If you find this post exciting, find more exciting posts on Learnhub Blog; we write everything tech from Cloud computing to Frontend Dev, Cybersecurity, AI, and Blockchain.

Resource

Top comments (2)

Collapse
 
sreno77 profile image
Scott Reno

It's cool to see someone else's take on this. I made something similar a few years ago Madlibs

Collapse
 
scofieldidehen profile image
Scofield Idehen

Really, care to share!!