Tower of Hanoi, response issue

So this is the code from the tower of hanoi project and I have been trying to debug this code for a long time, but I am hit with the same error again, well not exactly an error but whenever I try to type in my response of where I ‘want to’ place the disk , I get the response “Invalid Move, try again!”, which ,of course , shouldn’t be the case. I’ve tried to look into “#Play game” part of the code multiple times .but the code is the same as from the ‘unstuck video’…I would very much like to know where exactly I am going wrong…`(…and I am aware there is a similar topic related to mine on the forum but I really couldn’t gain anything from that conversation or how he got the correct response…)

from stack import Stack

print("\nLet's play Towers of Hanoi!!")

#Create the Stacks
stacks = []
left_stack, middle_stack, right_stack = Stack("Left"),Stack("Middle"),Stack("Right")

stacks.append(left_stack)
stacks.append(middle_stack)
stacks.append(right_stack)
#Set up the Game
num_disks = int(input("\nHow many disks do you want to play with?\n"))

while num_disks < 3:
  num_disks = int(input("Enter a number greater than or equal to 3\n"))

for disk in range(num_disks,0,-1):
  left_stack.push(disk)

num_optimal_moves = (2 ** num_disks) - 1
print("\nThe fastest you can solve this game is in {0} moves".format(num_optimal_moves))

#Get User Input
def get_input():
  choices = [stack.get_name()[0] for stack in stacks]
  while True:
    for i in range(len(stacks)):
      name = stacks[i].get_name()
      letter = choices[i]
      print("Enter {0} for {1}".format(letter,name))
    user_input = input("")
    if user_input in choices:
      for i in range(len(stacks)):
          return stacks[i]
   
#Play the Game
num_user_moves = 0
while right_stack.get_size() != num_disks:
  print("\n\n\n...Current Stacks...")
  for stack in stacks:
    stack.print_items()
  while True:
    print("\nWhich stack do you want to move from\n")
    from_stack = get_input()
    print("\nWhich stack do you want to move to\n")
    to_stack = get_input()
    if from_stack.get_size() == 0:
      print("\n\nInvalid Move. Try Again")
    elif to_stack.get_size() == 0 or from_stack.peek() < to_stack.peek():
      disk = from_stack.pop()
      to_stack.push(disk)
      num_user_moves += 1
      break
    else:
      print("\n\nInvalid Move, Try again!")
    
print("\n\nYou completed the game in {0} moves, and the optimal number of moves is {1}".format(num_user_moves,num_optimal_moves))



Set a break point. This can be done in many ways. One very simple and lightweight one is to use pdb (python debugger).

Run your file locally on your computer and place the trace statement where you want to see what’s going on. Example:

x = 5
import pdb; pdb.set_trace()
# rest of code

now when i run, when the interpreter hits the trace it’ll give me a prompt

(pdb)

there i can either query the values of existing variables in the namespace. Step, jump, continue to next breakpoint, and all the other normal debug queries. I’m sure there’s some documentation on pdb somewhere if you don’t know about these features.

so for example

(pdb) x
5

I can even change the value

(pdb) x = 6
(pdb) x
6

as well as query the type, and all that sort of jazz.

3 Likes