Search⌘ K
AI Features

Solution: Mad Lib Generator

Explore how to create a Mad Lib generator by defining a Python function that accepts parameters for nouns, verbs, and adjectives. Understand how to combine these inputs to build customizable and reusable sentence structures, enhancing your skills with functions and basic string operations.

We'll cover the following...

This program defines a function that creates a fun sentence using the words you give it.

  • def madlib(noun, verb, adjective): defines a function named madlib that takes three inputs (called parameters): a noun, a verb, and an adjective.

  • Inside the function, the print() statement combines these words to make a sentence.

  • When you call the function with madlib("cat", "dance", "silly"), the words fill in those spots:

    • noun"cat"

    • verb"dance"

    • adjective"silly"

Python
def madlib(noun, verb, adjective):
print("The", adjective, noun, "decided to", verb, "all day.")
madlib("cat", "dance", "silly")