Module 04 • Loops
RepeatingActions
ManoBot often needs to perform the same instruction again and again. Python loops let us repeat actions without writing the same code many times.
Watch
Mano has a repetitive problem.
Watch why writing the same instruction again and again is inefficient, and how loops solve the problem.
The Big Idea
Repeat once.Let Python do the rest.
A loop repeats a block of code. This keeps programs shorter, clearer and easier to change.
Without a Loop
move_forward()
move_forward()
move_forward()
move_forward()
With a Loop
for step in range(4):
move_forward()
Try It
Make ManoBotrepeat the move.
Choose a loop, change the number of repetitions and watch each loop iteration become one movement.
Repetitions
ManoCity Road
One loop = one move.
Ready to repeat?
Press Run Code and watch each loop iteration move ManoBot.
Two Types of Loop
Know how many?Or know when to stop?
I know how many times.
Use a for loop when the number of repetitions is known.
for step in range(5):
move_forward()
I know when to stop.
Use a while loop when repetition depends on a condition.
while destination_reached == False:
move_forward()
Mini Challenge
ManoBot must flash 3 times.
What number should go inside range()?
for flash in range(?):
print("Light ON")
Remember This