Search⌘ K
AI Features

Solution: Mad Lib Generator

Explore how to create a Mad Lib generator by defining and calling a Python function with parameters for noun, verb, and adjective. Understand how to use functions to build interactive, reusable program components that combine inputs into sentences.

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")