Python Development Course
Chapter
>
Level

Learning Loops
While Loops

Loops are sequences you can use to repeat the same code. This makes it easier for you to complete tasks in the game without having to write long lists of codes. There are two types of loops: while loops and for loops which we will be exploring in the following levels.

There are five (5) eggs in this map, you can pick them up with only a few lines of code using a while loop. Try to grab all the eggs by repeating the same two lines of code four times.

Guide

Objective

Grab all the eggs in the barn by only writing six (6) lines of code, using the while loop.

The chickens laid their eggs everywhere! Walk through the barn and collect all the eggs quick!

In order to reduce the amount of code you need to write and make coding a lot less tedious you can use loops! These are sequences that allow you to repeat the same code eliminating the necessity for writing the same code over and over again.

A while loop is one such loop where you can set code to repeat while a certain condition is present. Set up a variable named count to determine how many times you wish for code to loop then set it as a condition for the loop.

count = 0 while count < 4: [Code you wish to loop four times] count += 1

In this example the while loops checks if count is less < than 4 , count starts off as 0 and increases +1 each cyle until it reaches 4. Any code you insert indented under the while loop will be repeated, in this case four times. Be warned, loops can run forever if not stopped, so be sure to not delete or comment the variable count , otherwise it may trigger an error.

Try it out! Collect all five (5) eggs and complete the level by only having six (6) lines of code total written in the code editor.

Code book