LISTS IN SNAP What is a List? • A list is an ordered set of elements, where each value is identified by an index • Elements can be anything (numbers, letters, words, pictures, lists, etc.) Lists in Snap • On the Variables tab, make a variable, and give it a name • Set it to a List block (below) • Show and hide the list on the screen • You can add to the list when it’s on screen: • Create a list in your script with the list reporter block: • Can be used without creating a list variable first • Use the arrows to add/remove items, and type them in • Can be set to a variable (that wasn’t originally a list): Working with Lists and Elements • Add an element to the end • Remove an element • Insert an element at index • Replace an element at index • Retrieve/report an element • Get the number of elements • See if a list contains an element • Convert list to text • Make a copy of a list Walking lists • Use code to visit each element • Create variables for index and length • Use a Repeat loop counting up until index > length • Inside the loop, do your work • Could also use our custom For loop we made awhile back… Or, using a For each loop, built just for lists Extra List blocks • On the File menu, choose Import tools • You now have new blocks on the Variables and Operators tabs that work with lists • Explore using these, as you'll need them in the Hangman project • For loop is added here as well (so you don't have to use the custom block we built) Assignment • No handout today, just a practice coding assignment • Create a variable, set it to a list • Create a new block AddItems to add up the items in the list • AddItems should walk each element, adding to a Sum variable, and report it back • What kind of custom block is AddItems (Command, Reporter, or Predicate)? • What are its inputs? • What does it report back? Remember to set this before you’re done… • Extra challenges • Check to make sure the inputs are numbers or handle errors
© Copyright 2026 Paperzz