Python Stacks: Towers of Hanoi defaulting to "Invalid Move"

Hi there fellow Codecademy members,

I’m having a bit of a struggle debugging my Towers of Hanoi project in the stacks portion of linear data structures for Python3 (comp sci path). The code runs/functions with error messages, however, regardless of what the input for “from” and “to” stacks is/are, the code defaults to the else statement and prints: “Invalid Move. Try again.” I’ve followed along with the developer video and can’t seem to find the error in my code. I added code to print to_stack.is_empty() and from_stack.is_empty() to the terminal, and both return “False” whereas I’d expect the to_stack to return True, since the middle and right stacks show as empty when the game is started. Maybe (and hopefully) there is some syntax that I am overlooking, but I suspect the error lies in the get_input() function.

Here is the code:

So from what your describing, on the surface it seems like a control flow issue. You need to do a little digging before getting to the root of the problem.

I would print out whatever conditional is not being met just before it reaches it’s condition. I’ll show you a generic example, but you might have to tweak it to best suit your needs.

For example:

print(condition == True)

if (condition == True):

Thank you for the suggestion, I will have to try giving it another look with this in mind.

After rereading my original post, I noticed a typo (not sure if you can edit posts?) where I state that the code “runs/functions with error messages” and this should say without error messages. The code loops through as it should, doesn’t throw any error message so it’s more than likely in the logic.


I think there’s a distinction between a code running (compiling) and a code not having errors. In this case it may be the conditional logic, but once the logic is fixed it may uncover some errors that are not triggered because the conditions weren’t allowing for it to happen.


array = []

def function(array):
   #code with a potential error

if 1 == 2:

As long as the syntax is correct, this code will compile perfectly (usually). The only thing that will trigger an error is invoking function() but that never happens with this condition (in this example). For example, if the function tries to do an operation out of the array’s range, that would throw an error (whether it’s because of the input or because of the code inside the function would have to be seen on a case-by-case basis).

I just mention all of this to say that logically it’s not always sounds to assume:

"code compiles" and there's an undesired result --> error can only be logical

it’s more accurate to say:

"code compiles" and there's an undesired result --> current syntax is compiling AND
(there is a logical error OR there is a compiling error that's not yet apparent)
#note this last close allows for both sides of the OR statement to be true

As a side note, if following video tutorials I find it useful to tinker and debug early. Almost with every new function written. This is to make sure bugs don’t get hidden needlessly on top of a lot of code. It’s useful to print variables, print variable types, and also to call any new methods written and loosely get an idea of how the logic works with each one of them.

Heya! @brbeck1902

I was having a similar issue, everything was coming up as invalid!

What i figured out is that the get_input function was the root of my issue

in the lesson, we were shown

    if user_input in choices:
      for i in range(len(stacks)):
        return stacks[i]

and i couldn’t figure out exactly what was going on here, and i suspected it there was an issue assigning the stacks as to and from.

I rewrote those last 3 line into this, and have had no issues since

        if user_input in choices:
            return stacks[choices.index(user_input)]

this new line, is looking for the stack, in the list of stacks, that has the same index value as the list of choices!

maybe there is a better reason to use the original setup, but im still not even sure what it is trying to accomplish, but im also very green so i am likely overlooking something!

Dont give up! <3

Hi and thank you for sharing! I believe I did give up on it, hah. I suspected something was up in the get_input() function, and noted in my code that for any given input, it would assign both the from_stack and to_stack as left stack, which explains why its reading invalid move, but not why it’s assigned both to left in the first place… But yes, you are 100% right in that it appears to be an assignment issue.

It’s odd, because even following the project walkthrough to the T, it didn’t work. I’m not exactly sure what to make of that “i in range(len(stacks))” either, but I did just swap that out for your suggestion, and the code runs flawlessly now! I appreciate you taking the time to comment here, and hope this information helps others down the road.