Shopping List App

Add somewhere: "you can double-click on a list watcher to open it in a dialog box which you can then keep around..." --Jens SnapCon 2023

In this lab, you will create tools for storing and accessing data.

On this page, you will create a shopping list app.

Some programming languages use the word array instead of list.

Many computer applications—contact lists, playlists, calendars, reminders—involve manipulating lists of information using tools that search, sort, or change the items on the list. You've worked with lists before as you customized the gossip project.

AAP-1.C.1 second sentence, AAP-1.D.7
The list expression list{Señora, Ms. C, my cat, Hannah, Jake} would be written as
[Señora, Ms. C, my cat, Hannah, Jake]
. The items are positioned in the list in the order they appear in the text: "Señora" has index 1, "Ms. C" has index 2, and so on.
    Remember to Do Pair Programming Swaps
  1. Click here to load this file. Then save it to your Snap! account. This project contains button costumes for the sprites that the user will click to control the items on their shopping list.
  2. You learned how to initialize (create and set) a global variable on Unit 2 Lab 1 Page 4: Keeping Score with Global Variables.
  3. AAP-1.C
    Initialize a shopping list variable to store the information.
    1. Make a global variable called shopping list.
    2. Set shopping list to an empty list (a list with nothing in it). You will need a list () block.
      Use its left arrow to get rid of the input slot so that it looks like this: list. That way, you will have an empty list rather than a list with one empty item.
AAP-1.D.7 bullet 2
The assignment instruction set (shopping list) to (list) would be written as
shoppingList ← []
or shoppingList ← [].
AAP-1.D.7 bullet 1
The assignment instruction set (shopping list) to {apples, bread, carrots, rice, pasta} would be written as
shoppingList ← [apples, bread, carrots, rice, pasta]
or shoppingList ← [apples, bread, carrots, rice, pasta]. (In this app, you'll want the shopping list to begin empty, and then the user will add or insert additional grocery items one at a time.)

Adding Items to a List

AAP-1.C.2

An element is another name for an item in a list. (If the same value is in the list twice, that counts as two different elements.) Each element has a unique index (position) in the list.

You can use the insert (thing) at (1) of 'list input slot' or add (thing) to 'list input slot' block to add elements to a list.

AAP-1.D.6

Assigning a list to a variable lets you use one name to represent all the elements of a list as a unit.

AAP-2.N.1 bullet 4
insert (tomatoes) at (2) of (shopping list) would be written as
INSERT(shoppingList, 2, "tomatoes")
or INSERT(shoppingList, 2, 'tomatoes').
AAP-2.N.1 bullet 5
add (tomatoes) to (shopping list) would be written as
APPEND(shoppingList, "tomatoes")
or APPEND(shoppingList, 'tomatoes').
    AAP-2.N part a

    You've seen the ask and answer blocks on Unit 2 Lab 1 Page 2: Checking the Player's Guess.

    You've worked with multiple sprites on Unit 1 Lab 2 Page 2: Making Programs Talk.

  1. Write a script for the "Add Item" button sprite so that when that sprite is clicked, it will ask the user for a new item, and then put the user's answer in the grocery list.
  2. Test your "Add Item" button several times and fix any problems.

Removing Items from a List

  1. Write a script for the "Clear List" button sprite that asks the user if they're sure and then sets shopping list back to an empty list.
  2. Test your "Clear List" button and fix it if needed.

You can also remove items from a list using delete (1) of 'list input slot'. The delete block takes an item number and a list as input and it removes the item at that position from the list.

AAP-2.N.1 bullet 6
delete item (2) of (shopping list) would be written as
REMOVE(shoppingList, 2)
or REMOVE(shoppingList, 2).
  1. Write a script for the "Delete Item" button sprite so that when that sprite is clicked, it will ask the user to "Enter the number of the grocery item you wish to delete," and then remove the item with that number from the grocery list.
  2. Test your "Delete Item" button.
The items in a list are values, so you can use item of anywhere you can use any other expression. For example:

When you run this script in Snap!, the second line of code assigns to shopping list 2 the value of shopping list (that is, the same list, not a copy). So, the third line of code modifies both variables:
set(shopping list) to (list(apple)(banana))
set(shopping list 2) to (shopping list)
add (carrot) to (shopping list) shopping list watcher showing the contents of the variable to be apple, banana, carrot; and the shopping list 2 watcher showing the contents of the variable to be apple, banana, carrot

AAP-1.D.7 bullet 3, AAP-2.N.2
However on the exam, the statement
shoppingList2 ← shoppingList
makes a copy of the list. So modifying one of them does not modify the other.

The rules for how to use lists and how they behave differ depending on the programming language you are using.