Loops: Repeat Magic!
Can you make a robot jump 100 times with just 3 blocks?
Real Python code
This is the code from the video. Read it out loud like a recipe, one line at a time!
for count in range(5):
print("Jump!", count + 1)
print("Yay! All done!")
Quiz time!
How many times will this say Hi!?
for i in range(3):
print("Hi!")
Where loops hide
Games
Move every character, many times a second
Drawing
Repeat 4 times: draw a line, turn. A square!
AI
Practise on examples again and again
Chat apps
Show every message, one by one
What we learned
The long way
- 100 jump blocks
- Easy to make mistakes
- Hard to change
With a loop
- 1 repeat block
- Short and tidy
- Change just one number
Loops = repeat magic!
Try it yourself!
Open Scratch and make a sprite dance with a repeat block.
Bit is working on the next one: Variables: boxes that remember things