Loops - Avoid Repeated Work
python ·What are Loops?
Loops are a way to repeat code multiple times without having to write the same instructions over and over again. Think of loops like a recipe that says “repeat steps 3-5 three times” - instead of writing those steps three separate times, you just write them once and tell the computer to repeat them.
basic.show_string("Hello!")
basic.show_string("Hello!")
basic.show_string("Hello!")
basic.show_string("Hello!")
basic.show_string("Hello!")
This is repetitive and boring to write!
What if…?
- We wanted to show “Hello!” 100 times?
- We wanted to count from 1 to 50?
- We wanted to make a beating heart animation?
Writing the same code over and over would take FOREVER! 😴
Loops to the rescue! A loop repeats code multiple times automatically.
Types of Loops
There are two main types:
- for loops - repeat a specific number of times
- while loops - repeat while a condition is true
For Loops: The Basics
for i in range(5):
basic.show_string("Hello!")
This shows “Hello!” 5 times!
Let’s break it down:
for- starts the loopi- a variable that counts (like a counter)range(5)- repeat 5 times (0, 1, 2, 3, 4):- everything after this gets repeated- Indentation matters! - code inside the loop must be indented
Pop Quiz!
How many times will this show “Coding is fun!”?
for i in range(3):
basic.show_string("Coding is fun!")
Answer
3 times!
range(3) creates: 0, 1, 2 (that’s 3 numbers!)
“I” Am a Counter Variable
The variable i counts each loop:
for i in range(3):
basic.show_string("Loop number:")
basic.show_number(i)
This will show:
- Loop number: 0
- Loop number: 1
- Loop number: 2
Counting from 1 to 10
for i in range(1, 11):
basic.show_number(i)
range(1, 11) means start at 1, stop before 11
So it counts: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Remember: the second number is NOT included!
Your Turn!
What should go in the question marks to count from 5 to 8?
for i in range(??, ??):
basic.show_number(i)
Answer
for i in range(5, 9):
basic.show_number(i)Loops with Variables
We can use variables in our loops too!
name = "Emma"
for i in range(3):
basic.show_string("Hi " + name + "!")
This shows “Hi Emma!” three times.
The + combines (concatenates) strings together!
Forever Loops
Sometimes we want code to repeat forever:
while True:
basic.show_string("I never stop!")
Be careful! This really will run forever until you reset the micro:bit!
Building Our Beating Heart 💓
Let’s make a heart that beats forever using what we’ve learned:
- Show a big heart
- Show a small heart
- Repeat forever!
We’ll need:
- A forever loop (
while True) - Heart icons
- A small pause between beats
Step 1: Set Up
- Navigate to makecode.microbit.org
- Click on “New Project”
- Change to Python
- Delete the current code
Ready to code! 👩💻
Step 2: Create the Forever Loop
We want our heart to beat forever. To accomplish this we will need a while loop like we discussed earlier.
while True:
# Our heart beating code will go here
The # creates a comment - notes for humans that the computer ignores!
Step 3: Show Heart Icons
while True:
basic.show_icon(IconNames.HEART)
basic.pause(300)
basic.show_icon(IconNames.SMALL_HEART)
basic.pause(300)
basic.show_icon()displays built-in picturesIconNames.HEARTis a big heart ❤️IconNames.SMALL_HEARTis a small heart 🤍basic.pause(300)waits 300 milliseconds (0.3 seconds)
Complete Beating Heart Code
while True:
basic.show_icon(IconNames.HEART)
basic.pause(300)
basic.show_icon(IconNames.SMALL_HEART)
basic.pause(300)
Try it out! Your micro:bit should show a beating heart that never stops! 💓
Pop Quiz!
What would happen if we removed the basic.pause(300) lines?
Answer
B. The heart would beat very fast
Without pauses, the micro:bit switches between hearts so quickly you might not see the animation!
Customize Your Heart
Try changing these values:
basic.pause(100)- faster heartbeat 💨basic.pause(1000)- slower heartbeat 🐌- Add your name:
basic.show_string("Emma's heart") - Try different icons:
IconNames.HAPPY,IconNames.STAR
Extra Challenges
Try these challenges:
- Make the heart beat faster, then slower
- Add a message before the heart starts beating
- Make the heart beat 10 times, then stop
- Create your own custom animation pattern
- Use what you know about loops, variables, and pauses!
What We Learned Today
✅ Loops repeat code automatically
✅ for loops repeat a specific number of times
✅ while loops can repeat forever
✅ Indentation is important in Python
✅ range() helps us count
✅ We can combine loops with variables and strings