You should check if the stack is empty before you access it:<br><br>def popNum(num):<br> &nbsp; &nbsp;if len(stackA) &gt; 0 and num == stackA[-1]:<br> &nbsp; &nbsp; &nbsp; &nbsp;stackA.pop()<br>&nbsp;&nbsp; #etc<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --Michael<br clear="all"><br>-- <br>Michael Langford
<br>Phone: 404-386-0495<br>Consulting: <a href="http://www.TierOneDesign.com/">http://www.TierOneDesign.com/</a><br>Entertaining: <a href="http://www.ThisIsYourCruiseDirectorSpeaking.com">http://www.ThisIsYourCruiseDirectorSpeaking.com
</a>
<br><br><div><span class="gmail_quote">On 9/19/07, <b class="gmail_sendername">bhaaluu</b> &lt;<a href="mailto:bhaaluu@gmail.com">bhaaluu@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Greetings,<br>I&#39;m working with Python 2.4.3 on the GNU/Linux platform.<br>I&#39;m currently playing with functions and stacks, and am<br>using the &quot;Towers of Hanoi&quot; as a test game to play with.<br>Note: This game is not a recursive programming exercise,
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; it is meant to be solved by the player, manually.<br><br>The game asks for a number to move, and a stack to move it to.<br>The game starts out in this state:<br>=========================<br>A [5, 4, 3, 2, 1] False
<br>B [] True<br>C [] True<br>=========================<br>Enter number and stack to move it to: 1b<br><br>It seems to work fine with my test moves:<br><br>1b 2c 1c 3b 1a 2b 1b 1b 4c 1c 2a 1a 3c 1b 2c 1c 5b<br>Which brings the game to this state:
<br><br>Enter number and stack to move it to: 5b<br>5 B<br>A [] True<br>B [5] False<br>C [4, 3, 2, 1] False<br>=========================<br><br>Then, in the second round of test moves, it bombs on the first move:<br><br>Enter number and stack to move it to: 1b
<br>1 B<br>Traceback (most recent call last):<br>&nbsp;&nbsp;File &quot;funcStack.py&quot;, line 73, in ?<br>&nbsp;&nbsp;&nbsp;&nbsp;moveNum(num,abc)<br>&nbsp;&nbsp;File &quot;funcStack.py&quot;, line 52, in moveNum<br>&nbsp;&nbsp;&nbsp;&nbsp;popNum(num)<br>&nbsp;&nbsp;File &quot;funcStack.py
&quot;, line 32, in popNum<br>&nbsp;&nbsp;&nbsp;&nbsp;if num == stackA[-1]:<br>IndexError: list index out of range<br><br>The second round of test moves are:<br>1b 2a 1a 3b 1c 2b 1b 4a 1a 2c 1c 3a 1b 2a 1a 5c<br>The third round of test moves are:
<br>1b 2c 1c 3b 1a 2b 1b 1b 4c 1c 2a 1a 3c 1b 2c 1c<br>Which should bring the game to this state:<br>=========================<br>C [] True<br>B [] True<br>A [5, 4, 3, 2, 1] False<br>=========================<br><br>Here is my Python source code:
<br># funcStacks.py<br># passing parameters, returning values, pushing and popping<br># 2007-09-19<br># b h a a l u u at g m a i l dot c o m<br>######################################<br>&quot;&quot;&quot;<br>Pseudocode<br>
 1. Get the number to move and the column to move it to: 1b, 2c, etc.<br> 2. Find the column the number is in: A, B, or C<br> 3. Is it the last number (ie. can it be popped?) num == stackA[-1]<br> 4.&nbsp;&nbsp; If the number can be popped, pop it!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
stackA.pop()<br> 5.&nbsp;&nbsp; Else, number is buried: return an error and try again.<br> 6. Determine if the number can be legally moved to the column requested.<br> 7.&nbsp;&nbsp; If the column is empty, move the number:&nbsp;&nbsp;&nbsp;&nbsp;stackB.append(num)
<br> 8.&nbsp;&nbsp; If the column has a number, is it larger?&nbsp;&nbsp; stackB.append(num)<br> 9.&nbsp;&nbsp; Else if number in column is smaller,&nbsp;&nbsp;return an error and try again.<br>10. Loop.<br>&quot;&quot;&quot;<br>num = 0<br>abc = &quot;&quot;<br>
EMPTY = []<br>stackA = []<br>stackB = []<br>stackC = []<br><br>def isEmpty(stack):<br>&nbsp;&nbsp;&nbsp;&nbsp;if stack == EMPTY:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return True<br>&nbsp;&nbsp;&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return False<br><br>def popNum(num):<br>&nbsp;&nbsp;&nbsp;&nbsp;if num == stackA[-1]:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stackA.pop()<br>&nbsp;&nbsp;&nbsp;&nbsp;elif num == stackB[-1]:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stackB.pop()<br>&nbsp;&nbsp;&nbsp;&nbsp;elif num == stackC[-1]:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stackC.pop()<br>&nbsp;&nbsp;&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &quot;Error: number not available.&quot;<br><br>def pushNum(num,abc):
<br>&nbsp;&nbsp;&nbsp;&nbsp;if abc == &#39;C&#39;:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stackC.append(num)<br>&nbsp;&nbsp;&nbsp;&nbsp;elif abc == &#39;B&#39;:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stackB.append(num)<br>&nbsp;&nbsp;&nbsp;&nbsp;elif abc == &#39;A&#39;:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stackA.append(num)<br>&nbsp;&nbsp;&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &quot;Move not allowed, try again.&quot;
<br><br>def moveNum(num,abc):<br>&nbsp;&nbsp;&nbsp;&nbsp;popNum(num)<br>&nbsp;&nbsp;&nbsp;&nbsp;pushNum(num,abc)<br>&nbsp;&nbsp;&nbsp;&nbsp;return stack_status()<br><br>def stack_status():<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;A&quot;,stackA, isEmpty(stackA)<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;B&quot;,stackB, isEmpty(stackB)
<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;C&quot;,stackC, isEmpty(stackC)<br><br># main<br>print &#39;=&#39;*25<br>stackA=[5,4,3,2,1]<br>stack_status()<br>print &#39;=&#39;*25<br><br>myMove = raw_input(&#39;Enter number and stack to move it to: &#39;)
<br>while myMove != &#39;Q&#39;:<br>&nbsp;&nbsp;&nbsp;&nbsp;num = int(myMove[0])<br>&nbsp;&nbsp;&nbsp;&nbsp;abc = str.upper(myMove[1])<br>&nbsp;&nbsp;&nbsp;&nbsp;print num, abc<br>&nbsp;&nbsp;&nbsp;&nbsp;moveNum(num,abc)<br>&nbsp;&nbsp;&nbsp;&nbsp;print &#39;=&#39;*25<br>&nbsp;&nbsp;&nbsp;&nbsp;myMove = raw_input(&#39;Enter number and stack to move it to: &#39;)
<br><br>Remember, I&#39;m learning Python. I haven&#39;t studied OOP yet, so<br>suggestions to make a class out of it won&#39;t help me much. =)<br>--<br>bhaaluu at gmail dot com<br>_______________________________________________
<br>Tutor maillist&nbsp;&nbsp;-&nbsp;&nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br>