How to Create a Mad Libs Generator in Python?

Estimated read time 2 min read

A Mad Libs generator is a fun game where the user inputs different words to create a humorous story. Here’s how you can create a simple Mad Libs generator in Python:

  1. Define a story template with placeholders for the words the user will input. For example, “One day, I was [verb] in the [noun] when suddenly, a [adjective] [noun] appeared!”
  2. Use the input() function to prompt the user to enter different types of words, such as verbs, nouns, and adjectives.
  3. Replace the placeholders in the story template with the user’s input using string formatting.
  4. Print the resulting story.

Here’s an example of a Mad Libs generator in Python:

story = "One day, I was {verb} in the {noun} when suddenly, a {adjective} {noun} appeared!"

verb = input("Enter a verb: ")
noun1 = input("Enter a noun: ")
adjective = input("Enter an adjective: ")
noun2 = input("Enter another noun: ")

mad_libs = story.format(verb=verb, noun=noun1, adjective=adjective, noun=noun2)

print(mad_libs)

In this example, we define a story template with placeholders for the words the user will input. We then use the input() function to prompt the user to enter a verb, a noun, an adjective, and another noun.

We then use string formatting to replace the placeholders in the story template with the user’s input. Finally, we print the resulting story.

You can modify this example to include more placeholders and to create more complex stories. You could also use random words from a word bank or a list of pre-defined words instead of user input to create the Mad Libs story.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply