Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unfortunately you <code>operator_stack</code> list is empty therefore it returns a <code>IndexError</code> BTW if you want to find the last element of a list use and make sure to make it to <code>None</code> if its empty:</p> <p>So use:</p> <p><code>peek = operator_stack[-1] if operator_stack else None</code></p> <p>instead of: </p> <p><code>peek = operator_stack[len(operator_stack)-1]</code></p> <p>Also when debugging your code it is clearly visible from the comments that these lines in:</p> <p><em>line 49</em> :<code>while not operator_stack and prec[peek] &gt;= prec[element]:</code></p> <p><em>line 59</em> : <code>while not operator_stack:</code></p> <p>should actually look like:</p> <p><em>line 49</em> :<code>while operator_stack and prec[peek] &gt;= prec[element]:</code></p> <p><em>line 59</em> : <code>while operator_stack:</code></p> <p>Finally add a if statement to check if <code>peek</code> is <code>None</code></p> <p>A short version would be</p> <pre><code>#line 18 peek = operator_stack[-1] if operator_stack else None #line 49 if peek is not None: while operator_stack and prec[peek] &gt;= prec[element]: #Append the pop'd element of the operator stack to the #output_queue list. output_queue.append(operator_stack.pop()) #Append whatever is left (+,-,*,/) to the operator stack operator_stack.append(element) #line 59 while operator_stack: #Append the last element in the stack until the stack is empty. output_queue.append(operator_stack.pop()) </code></pre> <p>If you <a href="https://stackoverflow.com/questions/20129563/why-wont-my-list-populate-indexerror-list-out-of-range/20129672#comment30000816_20129672">doubt</a> what <code>while operator_stack:</code> means, see this simple example:</p> <pre><code>&gt;&gt;&gt; a = [2,5,6] &gt;&gt;&gt; while a: # a is not empty right? ... print 2 # so print 2 ... break # and break 2 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload