Have you ever played Mad Libs? It’s a hilarious word game where you fill in blanks with random words, creating funny and unexpected stories.
In this tutorial, we’ll build a Mad Libs Generator using Python! This project is perfect for beginners because it helps you practice user input, string manipulation, and print formatting—all essential Python skills.
By the end, you’ll have a fully functional Mad Libs game where users enter words, and the program generates a fun, personalized story. Let’s get started!
Step 1: Setting Up the Project
Before we start coding, let’s set up our Python project:
1. Make sure Python is installed on your computer. If not, download it from the official Python website.
2. Open your favorite code editor or IDE.
3. Create a new Python file, for example, mad_libs.py
.
Great, now, let's dive head first into our Python editor to get this build started.
Step 2: Understanding How Mad Libs Work
A Mad Libs game takes different types of words (like nouns, adjectives, and places) and inserts them into a predefined story template.
For example:
If the user provides these words:
- Noun: dog
- Adjective: happy
- Place: beach
The program might generate a sentence like:
"Be kind to your dog at the beach, where the weather is always happy."
Now, let’s code our own version!
Step 3: Taking User Input
We need to ask the user for words and store their answers in variables. In Python, we do this using the input()
function.
Let’s start by asking for different types of words:
# Asking the user for words
noun = input("Choose a noun: ")
p_noun = input("Choose a plural noun: ")
noun2 = input("Choose another noun: ")
place = input("Name a place: ")
adjective = input("Choose an adjective (a describing word): ")
noun3 = input("Choose one more noun: ")
Breaking It Down
input("Choose a noun: ")
→ This prompts the user to type a noun.- The response is stored in a variable (e.g.,
noun
,p_noun
, etc.). - We repeat this process for different parts of speech.
Try running the code so far. It should ask you for words and store them, but it won’t do anything with them yet.
Step 4: Creating the Story Format
Now that we have the user’s words, let’s insert them into a story template using Python’s print()
function.
# Printing the final Mad Libs story
print("\n------------------------------------------")
print("Be kind to your", noun, "- footed", p_noun)
print("For a duck may be somebody's", noun2 + ",")
print("Be kind to your", p_noun, "in", place)
print("Where the weather is always", adjective, ". \n")
print("You may think that this is the", noun3 + ",")
print("Well, it is.")
print("------------------------------------------")
How This Works
- The
print()
function displays the story. - We use variables inside
print()
to replace placeholders with the user’s words. - String concatenation (
+
) is used for punctuation where needed.
Try running the program now! After entering words, you’ll see a custom story generated with your inputs.
Step 5: Making It Look Nice
Let’s improve the user experience by adding:
- A welcoming message
- Line breaks for readability
Here’s the improved version:
# Mad Libs Generator
print("Welcome to the Mad Libs Generator!")
print("You'll be asked for different words to create a funny story. Let's go!\n")
# Asking the user for words
noun = input("Choose a noun: ")
p_noun = input("Choose a plural noun: ")
noun2 = input("Choose another noun: ")
place = input("Name a place: ")
adjective = input("Choose an adjective (a describing word): ")
noun3 = input("Choose one more noun: ")
# Printing the final Mad Libs story
print("\n------------------------------------------")
print("Be kind to your", noun, "- footed", p_noun)
print("For a duck may be somebody's", noun2 + ",")
print("Be kind to your", p_noun, "in", place)
print("Where the weather is always", adjective, ". \n")
print("You may think that this is the", noun3 + ",")
print("Well, it is.")
print("------------------------------------------")
Now the program feels more interactive and polished!
Step 6: Bonus Enhancements!
Want to make your Mad Libs game even cooler? Try these extra features:
Repeat the Game Automatically
Ask the user if they want to play again by using a simple while loop:
while True:
play_again = input("\nDo you want to play again? (yes/no): ").lower()
if play_again != "yes":
print("Thanks for playing!")
break
Use f-strings for Cleaner Code
Instead of string concatenation, use f-strings for easier formatting:
print(f"Be kind to your {noun} - footed {p_noun}")
print(f"For a duck may be somebody's {noun2},")
print(f"Be kind to your {p_noun} in {place}")
print(f"Where the weather is always {adjective}.")
print(f"You may think that this is the {noun3},")
print("Well, it is.")
Final Code: Mad Libs Generator
Here’s the complete, polished Mad Libs Generator:
# Mad Libs Generator
print("Welcome to the Mad Libs Generator!")
print("You'll be asked for different words to create a funny story. Let's go!\n")
while True:
# Asking the user for words
noun = input("Choose a noun: ")
p_noun = input("Choose a plural noun: ")
noun2 = input("Choose another noun: ")
place = input("Name a place: ")
adjective = input("Choose an adjective (a describing word): ")
noun3 = input("Choose one more noun: ")
# Printing the final Mad Libs story
print("\n------------------------------------------")
print(f"Be kind to your {noun} - footed {p_noun}")
print(f"For a duck may be somebody's {noun2},")
print(f"Be kind to your {p_noun} in {place}")
print(f"Where the weather is always {adjective}. \n")
print(f"You may think that this is the {noun3},")
print("Well, it is.")
print("------------------------------------------")
# Ask if the user wants to play again
play_again = input("\nDo you want to play again? (yes/no): ").lower()
if play_again != "yes":
print("Thanks for playing!")
break
Wrapping Up
Congratulations! You’ve built a fully functional Mad Libs Generator using Python.
This project helped you learn:
- How to get user input with input()
- How to store and use variables
- How to format strings with print()
- How to use loops to repeat a game
Next Steps: Improve Your Mad Libs Game!
- Add more creative Mad Libs templates
- Use different types of words (verbs, adverbs, animals, etc.)
- Save and print past Mad Libs stories
The best way to learn Python is by experimenting, so have fun tweaking your game!