ManoCity
LearningPython EssentialsRepeating Actions

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.

15 MinutesBeginner1 VideoInteractive Loops

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.

manobot_loop.py

Repetitions

5
1for step in range(5):
2move_forward()

ManoCity Road

One loop = one move.

🏠
🏫
🤖
0
1
2
3
4

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?

for

I know how many times.

Use a for loop when the number of repetitions is known.

for step in range(5):

move_forward()

while

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

Loops repeat instructionswithout repeating your code.

for → known repetitionswhile → repeat while true