title: Python Stack Out of Bounds Error
body: <p>I get an index out of bounds error on my <code>push</code> method for a stack.</p>
  
  <p>I'm trying to implement a postfix style logical calculator in python. Where for instance <code>0!1&amp;1!0=!1/|</code> will evaluate out to 1 or true. </p>
  
  <p>I implement this using a stack and the common methods associated with such a list "push" and "pop" But I'm not allowed to use the typical append or remove methods that one might fine in this sort of implementation. Instead I have to initiate the list to a standard size equal to the length of the expression that I will be performing my evaluation on.  </p>
  
  <p>I do this with my method called <code>evaluate</code> by referencing a global integer called <code>top</code> that points to the the top of the stack and is incremented or decremented within depending on whether I call <code>push</code> or <code>pop</code>.</p>
  
  <p>My <code>push</code> method is as follows</p>
  
  <pre><code>def push(stack, element):
      global tos
      tos += 1
      stack[tos] = element
  </code></pre>
  
  <p>My <code>pop</code> method is as follows</p>
  
  <pre><code>def pop(stack):
      #stack = None
      global tos
      tos -= 1
  </code></pre>
  
  <p>Essentially, I read through each individual element of the string using </p>
  
  <pre><code>for i in expression
  </code></pre>
  
  <p>Then I say, <code>if i ==</code> (some expression): perform a premade method.</p>
  
  <p>Here's are a couple examples from my code found within this loop that iterates through the expression</p>
  
  <pre><code>if op == "1":
      push(theStack, True)
  elif op == "0":
      push(theStack, False)
  elif op == "!":
      pop(None)
      push(theStack, Not(theStack[tos]))
  elif op == "&amp;":
      pop(None)
      pop(None)
      push(theStack, And(theStack[tos], theStack[tos + 1]))
  </code></pre>
  
  <p>I tested the <code>pop</code>, it seems do what is should. I think the error is with my <code>push</code>.</p>
  
  <p>I initiate <code>tos</code> to -1, also this is a quick peak at the top of my method that the loop is found in </p>
  
  <pre><code>def LogicalEval (expression):
      global tos
      theStack = [len(expression)]
  
      def Not(expr):
          if expr == True:
              return False
          else:
              return True
  
      def And (expr1, expr2):
          if expr1 == True and expr2 == True:
              return True
          else:
              return False
  </code></pre>
  
  <p>I do not initiate the stack at any point outside of <code>LogicalEval</code>, in fact I'm limited to just three main methods (<code>push</code>, <code>pop</code>, <code>LogicalEval</code>) outside of my helper methods such as <code>And</code> and <code>Not</code> that are found in <code>LogicalEval</code> </p>