Functions: Teach Bit a Trick!
Can Bit reuse a cheer without copying every step?
Real Python code
This is the code from the video. Read it out loud like a recipe, one line at a time!
def cheer():
print("Ready!")
print("Go!")
cheer()
cheer()
Quiz time!
How many times does Ready! print?
def cheer():
print("Ready!")
print("Go!")
cheer()
cheer()
Functions in real programs
Games
Call a function to reset a round.
Messages
Call a function to format a greeting.
Loops and functions
A loop repeats. A function names steps.
Define it. Call it. Reuse it.
Only define
- Name the trick
- Write its steps
- Steps are ready
Then call
- Use the name
- Run the steps
- Continue afterward
A function is a named, reusable group of steps.
Try it yourself!
On paper, name a two-step cheer and write two calls. Change one step in the definition, then follow both calls again.
Bit is working on the next one: Lists: keep several things together