Chapter 5: Madlibs Game¶
🚀 Open Notebook¶
📺 Video Tutorial¶
Math in Python is easy! 📐 (10:03)
📚 What You’ll Learn¶
Create a fun word game that combines user input and string formatting to generate silly stories!
🎯 Learning Objectives¶
Practice using multiple
input()statementsUse f-strings to create dynamic text
Understand how to structure an interactive program
Have fun with creative programming!
📖 Concept Explanation¶
What is Madlibs?¶
Madlibs is a phrasal template word game where:
Players are asked to provide words (noun, verb, adjective, etc.)
They DON’T know the story context
Their words are inserted into a pre-written story
The result is usually funny and unexpected!
Parts of Speech Reminder¶
Noun: Person, place, or thing (cat, pizza, teacher, ocean)
Verb: Action word (run, jump, sing, dance)
Adjective: Describes a noun (big, red, funny, delicious)
Adverb: Describes a verb (quickly, slowly, happily)
-ing Verb: Present continuous (running, jumping, singing)
How the Program Works¶
1. Ask user for words (without showing the story)
2. Store each word in a variable
3. Insert variables into story template using f-strings
4. Display the completed story
💡 Example Run¶
User Input:¶
Enter an adjective: sparkly
Enter a noun: elephant
Enter another adjective: purple
Enter a verb ending with 'ing': dancing
Enter a third adjective: confused
Output Story:¶
today i went to a sparkly zoo.
in an exhibit, i saw elephant
elephant was purple and dancing
I was confused!
✍️ Practice Exercises¶
Exercise 1: Extend the Story¶
Add more lines to the story! Ask for:
A color
An adverb (word ending in -ly)
A place
A food item
Exercise 2: Create Your Own Madlib¶
Write a completely new story template about:
Going to school
A superhero adventure
A cooking disaster
A space journey
Exercise 3: Themed Madlibs¶
Create madlibs for specific themes:
Horror Story: scary adjectives, spooky nouns
Romance: romantic verbs, lovely adjectives
Comedy: funny situations
💻 Code Template for Your Own Madlib¶
# Get user input
noun1 = input("Enter a noun: ")
verb1 = input("Enter a verb: ")
adjective1 = input("Enter an adjective: ")
place = input("Enter a place: ")
# Create your story
print(f"\nOnce upon a time, in a {adjective1} {place},")
print(f"there was a {noun1} who loved to {verb1}.")
print("The end!")
🎨 Story Templates to Try¶
Template 1: Adventure Story¶
character = input("Character name: ")
adjective = input("Adjective: ")
place = input("Place: ")
verb = input("Verb (past tense): ")
print(f"{character} went to the {adjective} {place}.")
print(f"There, {character} {verb} and found treasure!")
Template 2: News Report¶
city = input("City name: ")
noun = input("Noun: ")
number = input("Number: ")
adjective = input("Adjective: ")
print(f"BREAKING NEWS from {city}!")
print(f"A {adjective} {noun} was spotted {number} times today!")
🔍 Tips for Great Madlibs¶
1. Be Specific in Prompts¶
# Vague:
word = input("Enter a word: ") # User doesn't know what kind
# Better:
animal = input("Enter an animal: ") # Clear instruction
2. Use Descriptive Variable Names¶
# Confusing:
x = input("Enter something: ")
# Clear:
favorite_food = input("Enter your favorite food: ")
3. Add Context to Output¶
print("\n===== YOUR MADLIB STORY =====\n")
# ... story here ...
print("\n==============================")
🎮 Challenge Projects¶
Challenge 1: Multiple Endings¶
Create a madlib with 2-3 different endings that randomly select.
Challenge 2: Repeated Words¶
Ask for one word and use it multiple times in the story in different contexts.
Challenge 3: Long Story¶
Create a madlib that uses at least 10 different words and tells a complete story with beginning, middle, and end.
🚀 Try It Yourself¶
Run the existing madlib and try it with different words
Modify the story template to make it funnier
Create a brand new madlib about your favorite topic
Share your madlib with friends and see what funny stories they create!
📝 Key Learning Points¶
User input can be stored in many variables
F-strings make it easy to insert variables into text
The order of
input()statements determines the user experienceCreative prompts lead to funnier results
Programs can be fun and educational at the same time!
🎓 Key Takeaways from Video¶
Functions are reusable blocks of code
Import modules to use external code
Follow along with the video for hands-on practice
💡 These points cover the main concepts from the video tutorial to help reinforce your learning.
🔗 Next Chapter¶
Continue to Chapter 6: Arithmetic & Math to learn about mathematical operations in Python!