🎯 What you will learn
- A function can take inputs
- Inputs change what a function does
- Loops inside functions make longer dances
👋 Let’s get started!
What if a robot could dance a move 2 times, or 10 times, without you writing the code again? A function can take an input: a number that tells it how many times to repeat. 💃
🪜 Step by step
1. 🎁 Inputs
An input goes inside the brackets. The function uses it like a box of information.
def spin(times):
print("Spin", times)
2. 🔁 Loop inside
Put a loop inside a function to repeat the moves.
def spin(times):
for i in range(times):
print("Spin")
3. 🎶 Make a dance
Call your functions in order to build a whole dance.
spin(2)
wiggle(1)
🧪 Try it!
Change the code and press ▶ Run to see what happens. You can’t break anything!
Robot dance party
def spin(times):
for i in range(times):
print("Spin!")
def wiggle(times):
for i in range(times):
print("Wiggle!")
spin(2)
wiggle(1)
spin(1)🎨 Unplugged activity
💃 Invent a dance with 3 moves (for example: spin, clap, bow). Write it as functions, then perform it for your family. Ask them to call out a number of repeats!
Build the dance
spin(2)
wiggle(3)
spin(1)
print("Robot bows 🙇")📖 Key words
Tap a card to flip it over!
- Input
- Information given to a function 🎁
- range()
- Counts the repeats 🔢
- Parameter
- The name for an input inside a function 🏷️
🏆 Challenge
Call wiggle with 3 so the robot wiggles 3 times. Press ▶ Run.
Challenge
def wiggle(times):
for i in range(times):
print("Wiggle!")
Knowledge Check Quiz
- What does spin(3) send to the function?
- How many times does range(2) repeat?
- Why use a function for a dance move?