1

I have tried re-spacing the entire file so that each indent consists of 4 spaces rather then tabs, but I still get an invalid syntax error at the else statement in push(). In case it is relevant I am using sublime text 3 to develop. Any suggestions would be appreciated, thank you.

from Stack import Stack

class stackOfPlates:

    cap = 4
    def _init_(self):
        self.plates = []

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

    def push(self, item):
        p = Stack()
        if ((self.isEmpty()) or (self.plates[len(self.plates)-1].size() == cap)):
            p.push(item)
            self.plates.append(p)
        else:
            self.plates[len(self.plates)-1].push(item)

    def pop(self):
        temp = self.plates[len(self.plates)-1].pop()
        if(self.plates[len(self.plates)-1].isEmpty()): self.plates.pop()
        return temp

    def peek(self):
        return self.plates[len(self.plates)-1].peek()

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

Thanks @OdatNurd that solved my problem, there must have been something I was missing.

wAnder
  • 29
  • 3
  • Why are you allocating a new stack every time you call `push`? (And why are you even defining `stackOfPlates` instead of just using `Stack.Stack`? You seem to be implementing a stack of stacks. – chepner Apr 13 '18 at 20:04
  • i copy paste this code and i dont get any syntax error, is there maybe a hidden char in your source? try open it in vim? – sietse85 Apr 13 '18 at 20:04
  • tabs and four spaces are interchangeable, either one works, just make sure you keep it consistent throughout your program – Primusa Apr 13 '18 at 20:05
  • Possible duplicate of [What to do with "Unexpected indent" in python?](https://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python) – MooingRawr Apr 13 '18 at 20:05
  • Seems to compile fine for me; since you're using Sublime, my suggestion would be to click on the status line where it says `Spaces: 4` and select the `Convert indentation to Spaces` option. – OdatNurd Apr 13 '18 at 20:06
  • @chepner, I guess he is trying to implement by hand, a multi-stack with each component stack having a max size(cap). The class is expected to provide the required abstraction - this might be a popular interview question from Cracking the Coding Interview. – Spade Apr 13 '18 at 20:12
  • No error for me either. – Josep Valls Apr 13 '18 at 20:15
  • 1
    Since you solved your problem after converting indentation to spaces, it seems like you had mixed spaces and tabs. In Sublime Text, you can easily detect this by selecting the entire code and observing whether the indentation has 4 dots (i.e. 4 spaces) or a solid line (i.e. a tab). – Autonomous Apr 13 '18 at 20:34
  • The class `init` magic method is using only one `_` when it should be two. So, `_init_` should be `__init__` – mustachioed Apr 13 '18 at 21:31
  • @MustacheMoses thanks so much I was getting an error after fixing the indentation problem and this, in addition to moving capacity into the constructor fixed it for me – wAnder Apr 13 '18 at 22:26

0 Answers0