-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
30: Practice Exercise: Building a Mad Libs Generator
I've seen this happen a dozen times with students building their first interactive stories. You're feeling good, the logic is simple, and then you try to add a little bit of "smart" logic—like doing a quick calculation on a user's input—and the whole thing crashes. Let's look at a piece of code that looks perfectly fine at first glance but fails the moment you run it.
# Buggy Mad Libs Snippet
adj = input("Enter an adjective: ")
noun = input("Enter a noun: ")
amount = input("Enter a number: ")
# I want to say there's one more than the user entered
total = amount + 1
print("You have " + total + " " + adj + " " + noun + "s!")
The "Unsupported Operand" Crash
If you run that code, Python is going to throw a TypeError: can only concatenate str (not "int") to str (or something very similar). It feels weird because you clearly typed a number into the prompt, right? But here is the catch: input() always returns a string, regardless of what the user types. To Python, if you type "5", it isn't the number five; it's the text character '5'. You can't mathematically add 1 to a piece of text.
Casting Inputs and Using f-strings
To fix this, we need to "cast" the input into an integer using int(). While we're at it, I want to steer you away from using the + sign to glue strings together. It's clunky, easy to mess up with spacing, and slow. Instead, use f-strings. They allow you to plug variables directly into a string using curly braces, and they handle the conversion to text automatically.
# The Fixed Version
adj = input("Enter an adjective: ")
noun = input("Enter a noun: ")
# Wrap the input in int() to make it a number
amount = int(input("Enter a number: "))
total = amount + 1
# Use an f-string for a much cleaner layout
print(f"You have {total} {adj} {noun}s!")
Now, Python knows that amount is a number it can do math with, and the f-string handles the final display without us having to manually convert total back into a string. It's cleaner, more readable, and it actually works.
Designing Your Story Template
When you're building a full Mad Libs generator, the secret is to map out your variables before you start coding. If you just start writing input() calls, you'll likely lose track of which variable is which. I usually recommend a quick list like this:
- Place: (noun)
- Action: (verb)
- Feeling: (adjective)
Once you have your "ingredients," the actual Python code is just a matter of gathering those inputs and plugging them into your final story string. Just remember to keep your variable names descriptive; adj1 and adj2 are fine, but spooky_adj and funny_adj will save you a lot of headache as your story gets longer.
📋 Practical Task
Project: The Galactic Travel Brochure Generator
Your task is to build a Mad Libs generator that creates a travel advertisement for a fictional planet. Your program must meet the following requirements:
- Prompt the user for at least five different inputs (e.g., a planet name, a strange food, a futuristic adjective, a number, and a verb).
- Include at least one numerical input that you perform a simple math operation on (for example, calculating the travel time in years by multiplying the user's number by 2).
- Use f-strings to construct the final story paragraph.
- Print the completed "brochure" to the console in a way that looks like a cohesive advertisement.
There are no comments for now.