Mad Libs Generator project (code) in python

← Back to Projects

Mad Libs Generator in Python

About the project:This program code Generates a Mad Libs story by prompting the user for various words and inserting them into a predefined template.

Define the story template with placeholders for different word types.

Here, You can create any story template of your choice and put placeholders inside the template.

The placeholders are enclosed in curly braces {}.

you can Collect words from the user using input.


Project Level: Beginner

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: choose any code snippet and follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# Mad Libs Generator

def mad_libs():
    print("Welcome to the Mad Libs Generator!")
    print("Please provide the following words:\n")

    # Getting user inputs
    noun = input("Enter a noun: ")
    plural_noun = input("Enter a plural noun: ")
    verb_past = input("Enter a verb (past tense): ")
    adjective = input("Enter an adjective: ")
    place = input("Enter a place: ")
    celebrity = input("Enter the name of a celebrity: ")

    # Mad Libs story template - replaces the variables entered by user in this template
    story = f"""
    Today I went to the {place} with my best friend. 
    We saw a {adjective} {noun} jumping over some {plural_noun}.
    I {verb_past} so hard, I almost spilled my drink.
    Then, {celebrity} showed up and started dancing!
    It was the best day ever!
    """

    # Output the complete story
    print("\nHere's your Mad Libs story:")
    print(story)

# Run the function
if __name__ == "__main__":
    mad_libs()


Below is another code snippet with similar MadLibs generator code.

This Python code defines a story template with various placeholders. It then prompts user to enter different types of words (adjectives, nouns, verbs, adverbs), and finally, it fills in the blanks to present your unique Mad Libs story! You can run this code and follow the prompts in your terminal




# mad_libs_generator.py

def generate_mad_libs():
    """
    Generates a Mad Libs story by prompting the user for various words
    and inserting them into a predefined template.
    """
    print("Welcome to the Mad Libs Generator!")
    print("Please provide the following words to complete the story:\n")

    # Define the story template with placeholders for different word types.
    # The placeholders are enclosed in curly braces {}.
    story_template = """
    Once upon a time, in a {adjective_1} land, there lived a {noun_1} who loved to {verb_1}.
    One day, while walking through the {adjective_2} forest, they stumbled upon a {noun_2}.
    The {noun_2} suddenly started to {verb_2} very {adverb_1}.
    The {noun_1} was so {adjective_3} that they decided to {verb_3} the {noun_2} a {noun_3}.
    It was a truly {adjective_4} adventure, and everyone lived {adverb_2} ever after.
    """

    # Collect words from the user.
    # Each input prompt asks for a specific type of word.
    adjective_1 = input("Enter an adjective (e.g., 'sparkling'): ").strip()
    noun_1 = input("Enter a noun (e.g., 'wizard'): ").strip()
    verb_1 = input("Enter a verb (e.g., 'sing'): ").strip()
    adjective_2 = input("Enter another adjective (e.g., 'mysterious'): ").strip()
    noun_2 = input("Enter another noun (e.g., 'dragon'): ").strip()
    verb_2 = input("Enter another verb (e.g., 'dance'): ").strip()
    adverb_1 = input("Enter an adverb (e.g., 'quickly'): ").strip()
    adjective_3 = input("Enter a third adjective (e.g., 'astonished'): ").strip()
    verb_3 = input("Enter a third verb (e.g., 'offer'): ").strip()
    noun_3 = input("Enter a third noun (e.g., 'treasure'): ").strip()
    adjective_4 = input("Enter a fourth adjective (e.g., 'unforgettable'): ").strip()
    adverb_2 = input("Enter another adverb (e.g., 'happily'): ").strip()

    # Create a dictionary to hold the user's words,
    # mapping placeholder names to their actual values.
    mad_libs_words = {
        "adjective_1": adjective_1,
        "noun_1": noun_1,
        "verb_1": verb_1,
        "adjective_2": adjective_2,
        "noun_2": noun_2,
        "verb_2": verb_2,
        "adverb_1": adverb_1,
        "adjective_3": adjective_3,
        "verb_3": verb_3,
        "noun_3": noun_3,
        "adjective_4": adjective_4,
        "adverb_2": adverb_2,
    }

    # Format the story using the collected words.
    # The .format() method replaces the placeholders in the string
    # with the corresponding values from the mad_libs_words dictionary.
    completed_story = story_template.format(**mad_libs_words)

    print("\nHere is your completed Mad Libs story:\n")
    print(completed_story)

# This ensures that generate_mad_libs() is called only when the script is executed directly.
if __name__ == "__main__":
    generate_mad_libs()