๐Ÿ PyQuest๐Ÿญ Function Factory ยท ๐Ÿญ Invent Your Own Command
โญ 0 XP๐Ÿ Python is waking upโ€ฆ

Invent Your Own Command ๐Ÿญ

print(), len(), input() โ€” you've been using commands somebody else built. Today you build your own.

A function is a bundle of code with a name. You create one with def:

def cheer():
    print("GO TEAM GO!")
    print("๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰")

Three things to notice:

  • def means "define" โ€” you're inventing a new command
  • the parentheses () and the colon : are required
  • everything indented underneath belongs to the function

Here's the surprising part: running that code prints nothing! def only builds the function, like writing a recipe. To actually run it you call it by name:

cheer()
cheer()

Now it runs twice. That's the magic: write it once, use it forever. ๐Ÿ”

Your mission

  1. Build a function called cheer with def cheer():
  2. Make it print at least 2 lines
  3. Call it 3 times below the function

Your code

Press โ–ถ Run to see what your code does!