0

so I am trying to create a list of stack objects in Python. I have first created a class Stack that has simple methods that a Stack should have. I have then created another class called Stacks. I am trying to create a list of stacks. If a stack has more than 3 elements, it creates a new stack but I get an error when I try to display the elements. Could someone point out what I might be doing wrong here please?

class Stack:

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

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def printStack(self):
        for item in reversed(self.items):
            print (item)


class Stacks:

    def __init__(self):
        self.stacks = []
        self.noOfStacks = 0
        self.itemsOnStack = 0

    def dev(self):
        self.stacks.append(Stack())
        # if len(self.stacks) != 0:
        #     self.noOfStacks += 1

    def push(self, item):
        if self.itemsOnStack > 3:
            self.dev()
        else:
            self.itemsOnStack += 1
            self.stacks[self.noOfStacks].push(item)

    def pop(self, stackNo):
        return self.stacks(noOfStacks).pop()

    def size(self):
        return len(self.stacks)

    def printtack(self, index):
        print (len(self.stacks(index)))
        self.stacks(index).printStack()


stacky = Stacks()
stacky.dev()
stacky.push(3)
stacky.printtack(0)
3
  • What error are you getting? Commented Dec 29, 2017 at 14:38
  • Make sure you post Python code with correct indentation. Otherwise you are introducing new problems into the code. Commented Dec 29, 2017 at 14:45
  • FWIW, collections.deque can be used as a stack, and it's generally a little faster than using a list. Commented Dec 29, 2017 at 14:49

2 Answers 2

1

Indexing lists in Python works by [] not (). Try

 def printtack(self, index):
    self.stacks[index].printStack()
Sign up to request clarification or add additional context in comments.

Comments

0

One thing to note as kshikama said indexing should be done using [] not (),the other problem is using the len() method in the stack class u most override the __len__() method or as u have given use the size() method

class Stack:
    def __init__(self):
        self.items =  []

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def size(self):
        return len(self.items)

    def printStack(self):
        for item in reversed(self.items):
            print (item)

class Stacks:
    def __init__(self):
        self.stacks = []
        self.noOfStacks = 0
        self.itemsOnStack = 0

    def dev(self):
        self.stacks.append(Stack())
        #if len(self.stacks) != 0:
            #self.noOfStacks += 1

    def push(self, item):

        if self.itemsOnStack > 3:
            self.dev()
        else:
            self.itemsOnStack += 1
            self.stacks[self.noOfStacks].push(item)


    def pop(self, stackNo):
        return self.stacks(noOfStacks).pop()

    def size(self):
        return len(self.stacks)

    def printtack(self, index):
        print (self.stacks[index].size())
        self.stacks[index].printStack()
stacky = Stacks()
stacky.dev()
stacky.push(3)
stacky.printtack(0)

OUTPUT

1
3

1 Comment

Omg im such an idiot for using () as index instead of []. Thanks everyone above for helping me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.