Beginner Coding with Blocks
Chapter
>
Level

Functions and Procedures
Using lists in functions

Objective

Collect all vegetables in the field and use a custom function to pickle them.

Pickling is when you seal vegetables in a jar with vinegar and spices in order to give them flavor and preserve them for long periods of time. The field has several vegetables you can pickle using the press by the shed, collect all the vegetables and try it out!

First we would need to create a custom function named pickle_food that would allow us to pickle the vegetables using two (2) lists in order to put everything together.

Use the define function block to create a function named pickle_food with two parameters: food and amount. Inside the function, use a for loop block with variable x from 0 to 2. Inside the loop, use the speak block with a text join block that combines "You have pickled ", the amount from the list at index x (using get item from list with FROM_START and the x variable on the amount parameter), " ", and the food name from the list at index x (using get item from list with FROM_START and the x variable on the food parameter).

The custom function uses the lists as arguments, one for food and the other for amount. This means we need to create a list that stores the names of the food we wish to pickle and another list containing the amount of each food we wish to pickle. It's important to have a separate list for the amount as the number of vegetables you choose to pickle can vary.

Collect all the vegetables in the field and create a list named vegetables for the vegetable names using the create empty list block, then use the set item in list block with INSERT mode at LAST position to add "Cucumbers", "Tomatoes", and "Radishes". Create another list called vegetables_number for the amounts.

After populating the vegetables list, make variables for each vegetable and store the number you've acquired of each (cucumbers = 5, tomatoes = 3, radishes = 4), then create the vegetables_number list using the create list with block containing those three variables.

Once you have everything, head to the X mark and use the call function block to call pickle_food with the two lists you created as arguments in order to pickle the veggies and complete the level.

Code book