Python Development Course
Chapter
>
Level

Conditions
Alternate Loop Conditions

Objective

Collect all the berries in the courtyard using loops and combine them to make Jam.

Now that you’ve made your way into the courtyard, collect some berries on the field and take them to the mixer to produce Jam. Use loops to optimize your code and conditions to adapt the code to specific situations.

In order to combine the berries you must have a list, create a list named berries, like this: berries = []. Set this up before creating the loop so you may add the berries to your list using the append() function as you grab them.

Create a for loop with a range() of three (3) to make your way around the courtyard grabbing and storing the berries. There are three (3) different types of berries in the courtyard: red berries , blue berries and black berries (the purple berries) , each one with different quantities. Use an if statement for each loop cycle and store the specific quantity of each berry in a variable so you can append them in the list.

for x in range(3): player.move_forward(4) if x == 0: red_berries = 3 berries.append(red_berries) if x == 1: # Insert Code for blue berries if x == 2: # Insert Code for black berries player.turn_left()

Remember x determines what loop cycle is currently ongoing and it always starts at 0. In each cycle collect berries, create a variable for the berries you collect, store the correct quantity and append them like in the code above.

After you’re done collecting and storing the berries you can use an else statement to close out the for loop. Using the statement in this manner allows you to run one final strip of code triggered after the loop is done.

for x in range(3): ......... else: player.move_forward(4) # Insert combine code here

Set the movement to reach the X mark and mix the berries to form Jam using the combine() function, like this: player.combine(berries) , do this in order to complete the level.

Code book