Beginner Coding with Blocks
Chapter
>
Level

Advanced Lists and Data Structures
Functions with Nested Lists

Objective

Use a custom function that loops through the field, planting and watering crops.

The field has several trees that need planting, grab the bag containing seeds then refill your water by the well. Use a custom function with a nested list structure (dictionary) to loop through the field, and plant then water the seeds.

Grab the bag of seeds which will grant you the following tree seeds in a nested list structure: "orange : 3", "pear : 4", "apple : 3", "peach : 2". Walk to the light X mark next to the well and use the collect block with "water" to gather water.

Once you collect all necessary materials, create a custom function named sow_field using the define function block with three parameters: tree, loops, and direction. Inside the function, use a for loop block with variable x from 0 to loops - 1. Inside the loop, use if blocks to check if direction equals "right" or "left", and perform different movement and planting patterns based on the direction.

The first argument, tree is the name of the key used in the nested list structure, those being "orange", "pear", "apple" and "peach". Use the plant block with the seeds variable and the tree parameter as the key.

The next argument is loops and this determines the number of cycles you wish for the function to loop for. This is equal to the number of seeds of each type in the field. The last argument is direction, those being "left" and "right", choose which direction you would like the player to move towards when looping the function.

Head to the upper most part of the map after collecting the water and use the call function block to call sow_field with "orange", 3, and "right" as arguments. Do this for all tree seeds in order to complete the level.

Code book