Beginner Coding with Blocks
Chapter
>
Level

Conditions and Decisions
Checking for existence

Objective

Find if ingredients stored in containers and combine them to make food.

You need several ingredients to be able to make a salad but you need to collect them first. You'll find carrots, tomatoes, lettuce and cucumbers in the field, carrots and cucumbers however are stored away. Collect the items you can grab in the field.

In order to find the missing items head to the X marks in front of containers and test conditions to check if the items are stored in a container. Use a for loop block to make the process quicker and the collect block to retrieve any items.

Use the for loop block with a variable x that goes from 0 to 3. Inside the loop, use an if block to check if "carrot" or "cucumber" is in the bag constant. Use the find item in list block to check if items exist in the bag. If the condition is true, use the collect block with get item from list to get the item from bag at index x (use get item from list with FROM_START and the x variable).

Once you collect all items, store them inside a list named items using the create empty list block, and add numbers to the list using the set item in list block with INSERT mode at LAST position.

Finally, walk to the light X mark and combine all items for a meal using the combine block. Check if all items are accounted for by checking the length of your list in an if block using the length of list block. Use the less than comparison block to check if the length is less than 5. If true, use the combine block with your items list.

Code book