# Towers of Hanoi game not ending even when solved

``````from stack import Stack

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

#Create the Stacks
stacks = []
left_stack = Stack(name = 'Left')
middle_stack = Stack(name = 'Middle')
right_stack = Stack(name = '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 i in range(num_disks, 0, -1):
left_stack.push(i)
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(0, 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(0, len(stacks)):
if user_input == choices[i]:
return stacks[i]

#Play the Game
num_user_moves = 0
while (int(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.is_empty():
print("\n\nInvalid Move. Try Again")
elif to_stack.is_empty() or to_stack.peek() >= from_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))

``````

I’m learning Codecademy’s course in data structures and algorithms in python and I’m at the project where I have to use stacks to make a game called the Towers of Hanoi. The game runs and plays well right until the end where the game doesn’t end even when the puzzle has been solved. It kept asking my for more inputs. What I’m getting from this is that right_stack.get_size() is always different from num_disks. I’ve tried this with num_disks being 3,4, and 5. The solution for 3 is (L - R, L - M, R - M, L - R, M - L, M - R, L - R).

Here’s a screenshot of the rules of the game because I think they can explain it better than I can Would appreciate any ideas on how to solve this! Thank you!

The problem is in your ‘inner’ while loop on line 42. You have it indented in the ‘for’ loop prior to it. The ‘for’ loop that does the ‘stack.print_items()’ is a singular ‘for’ loop with nothing else in it.

while (int(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.is_empty(): print("\n\nInvalid Move. Try Again") elif to_stack.is_empty() or to_stack.peek() >= from_stack.peek(): disk = from_stack.pop() to_stack.push(disk) num_user_moves += 1 break else: print("\n\nInvalid Move. Try Again")
1 Like

Thank you for helping me This does solve the problem and I understand that the “for” loop there is just to print some messages before we make our next move in the puzzle. So what I had before was that the “for” loop had to go through all the stack in stacks (which are 3 iterations) in order for the outer ‘while’ loop to continue to its next iteration. Meaning I have to solve the game 3 times in order to break free from the “for” loop and for “(int(right_stack.get_size()) != num_disks)” condition to be considered again. Then it would stop the game. Am I missing something?

1 Like

Correct.
Also, you would have noticed with your original code, that the ‘stack.print_items()’ never printed the correct output. When I ran your code, that was the first indication that something was wrong and where I started looking.