Beginner Coding with Blocks
Chapter
>
Level

Conditions and Decisions
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 an empty list named berries using the create empty list block. Store it in a variable before creating the loop so you may add the berries to your list using the set item in list block with INSERT mode at LAST position as you grab them.

Create a for loop block with variable x from 0 to 2 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 if blocks for each loop cycle and store the specific quantity of each berry in a variable so you can append them to the list.

Use the for loop block. Inside the loop, move forward 4 steps, then use multiple if blocks to check the value of x. If x equals 0, set red_berries to 3 and add it to the list using the set item in list block with INSERT mode at LAST position. If x equals 1, set blue_berries to 5 and add it to the list. If x equals 2, set black_berries to 4 and add it to the list. Then 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 add them to the list using the set item in list block.

After you're done collecting and storing the berries, the code after the loop will execute. Move forward 4 steps to reach the X mark and mix the berries to form Jam using the combine block with your berries list to complete the level.

Code book