Python Development Course
Chapter
>
Level

Learning Loops
For Loop

Objective

Walk through the hedge maze to reach exit using for loop with no more than six (6) lines of code.

In order to get through the maze using no more than 6 lines of code, you need to use for loops. This is a similar to while loops but functions a bit different, for loops can only be done a predefined amount of times rather than using a condition like while loops.

for x in range(3): player.move_forward(2)

In this example the code player.move_forward(2) is repeated 3 times using a for loop. The x is a generic variable that stores how many times the for loop has cycled. The range() is how many times you want the loop to repeat in total. All indented code you put under the for loop code will be repeated.

Code book