A variable lets you store one value, such as a number, but what if you want to store a collection of numbers together? For example, maybe you have a list of numbers, such as scores from a Skeeball tournament. It wouldn't make sense to create a new variable for each number; all the numbers should be placed in one structure.
Programming languages provide the List item for storing lists of items together. A list is similar to a automatic-resizable shelf for storing collections of items.
Let's create a program to get a list of scores from the user. Once the list is complete, we can display useful info about it to the user.
- Create a list
scoresand assign it to a variable.
- Create a loop that will repeat 3 times. Later you can change your loop to a larger number or one that will continue until a condition is met.
- Inside the loop, use the prompt block to get input from the user as a number. Use the
in list insert-atblock to add this number to your list.
in list insert-at block. Change
insert-atin its dropwdown.
- Once the loop is done, let's gather some info from it. For now, let's print the sum and average of the numbers in the list. You can easily do this in Blockly with the sum-of-list block (located in the math blocks).
Below is the solution for steps 1-3. You just need to print the information from the list at the end.