computer4kids is now ComputerLeap. Same free lessons, same team. Read why we changed our name

← All lessons · 🧑‍💻 Coding Academy

💃

🎓

⏱ 15 min

Free

Robot Dance Moves

Functions with inputs let one dance move work with any number of spins.

🎯 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

  1. What does spin(3) send to the function?
  2. How many times does range(2) repeat?
  3. Why use a function for a dance move?
← Previous lesson
Next lesson →