You should check if the stack is empty before you access it:<br><br>def popNum(num):<br> if len(stackA) > 0 and num == stackA[-1]:<br> stackA.pop()<br> #etc<br><br> --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> <<a href="mailto:bhaaluu@gmail.com">bhaaluu@gmail.com</a>> 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'm working with Python 2.4.3 on the GNU/Linux platform.<br>I'm currently playing with functions and stacks, and am<br>using the "Towers of Hanoi" as a test game to play with.<br>Note: This game is not a recursive programming exercise,
<br> 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> File "funcStack.py", line 73, in ?<br> moveNum(num,abc)<br> File "funcStack.py", line 52, in moveNum<br> popNum(num)<br> File "funcStack.py
", line 32, in popNum<br> 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>"""<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. If the number can be popped, pop it!
stackA.pop()<br> 5. 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. If the column is empty, move the number: stackB.append(num)
<br> 8. If the column has a number, is it larger? stackB.append(num)<br> 9. Else if number in column is smaller, return an error and try again.<br>10. Loop.<br>"""<br>num = 0<br>abc = ""<br>
EMPTY = []<br>stackA = []<br>stackB = []<br>stackC = []<br><br>def isEmpty(stack):<br> if stack == EMPTY:<br> return True<br> else:<br> return False<br><br>def popNum(num):<br> if num == stackA[-1]:
<br> stackA.pop()<br> elif num == stackB[-1]:<br> stackB.pop()<br> elif num == stackC[-1]:<br> stackC.pop()<br> else:<br> return "Error: number not available."<br><br>def pushNum(num,abc):
<br> if abc == 'C':<br> stackC.append(num)<br> elif abc == 'B':<br> stackB.append(num)<br> elif abc == 'A':<br> stackA.append(num)<br> else:<br> return "Move not allowed, try again."
<br><br>def moveNum(num,abc):<br> popNum(num)<br> pushNum(num,abc)<br> return stack_status()<br><br>def stack_status():<br> print "A",stackA, isEmpty(stackA)<br> print "B",stackB, isEmpty(stackB)
<br> print "C",stackC, isEmpty(stackC)<br><br># main<br>print '='*25<br>stackA=[5,4,3,2,1]<br>stack_status()<br>print '='*25<br><br>myMove = raw_input('Enter number and stack to move it to: ')
<br>while myMove != 'Q':<br> num = int(myMove[0])<br> abc = str.upper(myMove[1])<br> print num, abc<br> moveNum(num,abc)<br> print '='*25<br> myMove = raw_input('Enter number and stack to move it to: ')
<br><br>Remember, I'm learning Python. I haven't studied OOP yet, so<br>suggestions to make a class out of it won't help me much. =)<br>--<br>bhaaluu at gmail dot com<br>_______________________________________________
<br>Tutor maillist - <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>