Skip to Content
Course content

30: Practice Exercise: Building a Mad Libs Generator

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.