Lists


Collapse Content

Beta page

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.

  1. Create a list scores and assign it to a variable.
  2. 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.
  3. Inside the loop, use the prompt block to get input from the user as a number. Use the in list insert-at block to add this number to your list.

    in list insert-at block. Change set to insert-at in its dropwdown.
  4. 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).
Stuck?

Contact Us
Sign in or email us at [email protected]