4

I have been trying to alter a list in class Inventory from class Pod, but I get an error that I am popping from an empty set. Is there anyway that I can pop from a list from an Inventory instance that I know is populated? Essentially, I am trying to transfer widgets from Inventory to Pod.

class Widget():

    def __init__(self):
        self.cost = 6
        self.value = 9


class Inventory():

    def __init__(self):
        self.widgets_inv = []
        self.cost_inv = 0
        self.value_inv = 0

    def buy_inv(self):
        x = int(input("How many widgets to you want to add to inventory? "))
        for i in range(0, x):
            self.widgets_inv.append(Widget())

    def get_inv(self):
        print("You have " + str(len(self.widgets_inv)) + " widgets in inventory.")

    def cost_of_inv(self):
        cost_inv = len(self.widgets_inv) * Widget().cost
        print("The current cost of your inventory is: " + cost_inv + " USD.")

    def value_of_inv(self):
        val_inv = len(self.widgets_inv) * Widget().value
        print("The current value of your inventory is: " + val_inv + " USD.")

class Pod():
    """A pod is a grouping of several widgets.  Widgets are sold in pods"""

    def __init__(self):
        self.pod = []

    def creat_pod(self):
        x = int(input("How many widgets would you like to place in this pod? "))
        for i in range(0, x):
            self.pod.append(Widget())
            Inventory().widgets_inv.pop()
5
  • Please include the error-message in your question. Please let us know on which line the error occurs. May be on the last line ? Commented Jul 3, 2017 at 20:07
  • Sorry about that. Commented Jul 3, 2017 at 20:45
  • ---> 41 Inventory().widgets_inv.pop() IndexError: pop from empty list Commented Jul 3, 2017 at 20:46
  • One way to prevent this error is to check whether there any elements in list widgets_inv. The number of entries can be fetched by calling len . Use an if-statement to check whether it is not 0. Only if not 0 use pop. Commented Jul 4, 2017 at 4:03
  • I think I know the problem, but I cannot figure out an answer. I believe the problem is that when I call Inventory() in Pod() it creates a new instance for Inventory(). Is there any way I can call a specific instance in Inventory() from Pod() that I know already has a populated list? -Thanks Commented Jul 4, 2017 at 14:45

1 Answer 1

6

You should modify the creat_pod-method, so that you can handover the Inventory-object. This allows you to add widgets to the inventory-object before calling creat_pod-method:

def creat_pod(self, inventory):
        x = int(input("How many widgets would you like to place in this pod? "))
        for i in range(0, x):
            self.pod.append(Widget())
            inventory.widgets_inv.pop()

In your original code you create always a new Inventory-object, which has therefore and empty widget-list:

Inventory().widgets_inv.pop()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Aedvald, one question, the 'inventory' that you have included as an argument, does that refer to the class or the instance? Same question for the final line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.