Your first function
Stop copying and pasting. Give a block of code a name and run it whenever you like.
Code runs right here in your browser. Nothing to install.
Learn it
Define a function once with def, then call it by name as often as you like.
A function is a named block of code that does one job. You have already used built-in functions like print(), input() and len(). Now you will make your own.
You define a function with def, a name, brackets and a colon. The code inside is indented. Defining a function does not run it. You run it by calling it: writing its name with brackets, like show_banner().
Functions make code shorter (write once, use many times), easier to read (a good name explains what happens) and easier to fix (change it in one place).
def show_banner(): print("*" * 20) print(" WELCOME BACK") print("*" * 20) show_banner() print("Loading level 1...") show_banner()
Try it yourself
+10 XPDefine a function called cheer that prints Go team! and then We can do this!. Call it three times.
Create a free account to start this lab
Sign in to write, run and check your code here. Your progress, XP and code are saved to your account. No card needed.
Capture the flag
+10 XPWhich keyword do you use to define a function?
Create a free account to start this lab
Sign in to answer the flag question. Your progress, XP and code are saved to your account. No card needed.
Border maker
+20 XPWrite a function border() that prints a line of 25 ~ characters. Use it to print a mini poster: a border, the line PYTHON CLUB, the line Thursdays 4pm, and another border.
Create a free account to start this lab
Sign in to do this homework and have it checked. Your progress, XP and code are saved to your account. No card needed.