Lesson 2

Stocking the Shelves

Now let’s make a dictionary to represent items in a shop. For our project today, we are going to make an item shoppe, complete with a cart that we can add items to.

In order to represent items and prices in this shop, we are going to use a dictionary, where the keys represent the item names, and the values associated with each are the prices of each item.

Briefly have students think about what kind of shop they want theirs to be, and then walk them through coding their own dictionaries.

For this example, we will be coding a Blacksmith and Bakery, so this example will add items in that fit that theme.

Starting from line 3, we can add ‘items’ into our shop. We do this by placing the item name inside of quotations, and then placing its price right next to it, seperated by a colon. Ex: “Donut”:10

Add Empty Dictionary

We’ve set up our items in the store, but now we need a ‘shopping cart’ to place the items in. We will accomplish this by setting up an empty Dictionary

On line 11 add the following code myCart={}

We will also need a way to tell the program that we are done shopping. On line 12 we will create a variable that will keep track of whether or not we are done shopping. Add keep_shopping = ‘y’

Checking our work

In order to test our program, we will have to try and ‘print’ out the price of one of our items.

On line 14 add the following code print(store[“Baguette”]). Make sure that instead of “Baguette” you use one of the items you defined earlier

When we run this program, it should print out the corresponding price value into the terminal. In this case, we get the value 1, because that was the price we set for the Baguette. Once you have tested it, make sure to remove this line of code from the program