Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Checking Items in an Inventory

00:00 Let’s look at a more practical example, and specifically you want to help your Victorian-era merchant friend to set up an e-commerce store because you have to go with the times, right?

00:11 And to do this, you’ll start by setting up an inventory, which you’ll implement as a dictionary that maps an item to the available quantity. Then you’ll work with a request_list, which will just be a list of user-selected items.

00:24 So this is what a customer wants to check out from the store, and you’ll implement the logic in inquire_availability(), so this will be just a function that checks whether the item is available.

00:36 And this is also where you’re going to implement your membership testing operations. I’ll head over to my IDE and let’s start coding this up.

00:45 So I have a new file here that I called shop.py, and I’ll just paste an inventory and a request_list so we don’t need to type that out.

00:53 But to talk over it quickly, you have this inventory, which is a dictionary that maps a couple of items that this merchant has at hand to the available quantity.

01:02 And you can see that not all of them are available. So we have mead, which is currently not available, and then you have a request_list from a customer of the items that they want to buy.

01:12 And you can see that there’s also a dagger in there, so this is maybe a slightly sinister customer and the merchant just doesn’t deal in daggers.

01:21 So there’s two specific situations that you’ll want to account for. One is that items may be generally available, but currently out of stock. And then there’s also items that just aren’t available in that store.

01:34 So let’s go ahead and start coding up the inquire_availability() function. Start with saying def inquire_availability()

01:45 and then we’ll pass in here ware, so an item. Let’s start by implementing this without any membership checking. So what’s a straightforward implementation?

01:56 You may want to check if the item is available, so let’s start with a conditional statement. This we can just do with dictionary lookups. So I can say if inventory[ware] is smaller or equal to zero, we want to give a message and we’ll just print it out.

02:16 So I type the print, open the parentheses and insert an f-string that says, "Forgive me, but the ware is not at hand." ware I’m interpolating into the string using the curly braces.

02:28 And then else I’ll say another message, we’re just going to inform the customer about the amount of the certain item that they’re looking for, how much is still available.

02:39 Okay, so this would be just a straightforward implementation of this where I’m not doing any membership checking. Let’s see what type of problem we could run into here, right?

02:48 I’ll just loop over the items that the person wants to purchase. So I’ll say for ware in request_list,

02:58 and then call inquire_availability()

03:03 on each of those wares.

03:06 So I will go ahead and run that file python shoppe.py. And we are running into an error. And let’s look at what happened here. So the customer asked for goblet, mead, rose, and dagger.

03:18 So first of all, the rest of the logic seems to be working. We get, 25 of goblets dost remain! so that’s what’s available in terms of goblets.

03:27 Then we get information, "Forgive me, but mead is not at hand." So it’s generally in the inventory, but we currently don’t have any.

03:34 And then roses, same as the first one. It just tells the customer there’s eight roses that are still left. So maybe you could make a nice bouquet out of that.

03:42 But then the customer is asking for a dagger. And currently the first thing that your code tries to do is to do a dictionary lookup and compare it to zero.

03:52 And in this case, the dictionary lookup fails, so you’re getting an error and your program quits. As always in Python, there are different ways that you could handle this, like for example with using .get() instead and the default value.

04:05 But we are here, of course, about membership checking. So let’s solve this issue by first checking whether the item is in the inventory at all.

Become a Member to join the conversation.