
Code
Your First Spell
A function is a named recipe of code. You write it once, then run it whenever you like by calling it.
def light_lantern():
print("Lantern lit!")
print("The dark retreats.")
light_lantern()
light_lantern()
defstarts a function definition, followed by a name, parentheses()and a colon:.- The indented lines are the function's body.
- Defining a function does not run it. The body only runs when
you call it with
light_lantern(). Don't forget the()!
Pip tip: Name functions after what they do:
light_lantern,count_coins,greet. Use lowercase words joined by_.
Assignment
- Define a function named
cheerthat printsHip hip!and thenHooray!(two lines). - Below the definition, call
cheer()twice.
- Define a function named
cheerthat printsHip hip!and thenHooray!(two lines). - Below the definition, call
cheer()twice.
main.py Python sleeping
Press Run to try your code, then Check to test it.