Towers of Hanoi Won't Accept Any Move I Make

I’m presuming it’s the problem. I certainly didn’t expect that would happen, and if its value keeps getting overwritten, then how can it return the left and right stacks? I just wanted your opinion.

There’s no guessing involved in that.

Should it be that way, or not?

No you don’t. That’s my point. You control this. You’re the architect.

You’re basically saying that somebody else is the designer of your code and that you don’t need to know what your code should do.

You need a different expectation of your level control and awareness of your code.

I need to know, of course. It’s just since I’m still learning, I occasionally need some advice to help me know when I hit a dead end.

so you did expect something? make it do whatever you expected.
if you see something that is different from what you expect then that is a bug, period

of course, your plan for what should happen might not actually add up, but that’s a completely separate problem from writing code. and, this problem has to be solved before writing code that’s not… in any way optional, because the only alternative is to write code without having decided what it should do
Infinite monkey theorem - Wikipedia

also, that problem is really just every-day logic, so that’s another reason why you don’t need/want/whatever my/anyone’s opinion on it, it’s something that you generally already know how to do because you can manually carry out that task by virtue of being a human

1 Like


You start out by deciding what should happen for some small piece of code. You need to flesh this out quite a bit, as if you were convincing somebody else that it would do the right thing. Or, convince yourself, rather. Does this strategy make sense? No point writing code without first sanity checking the idea for it.

Then you write code for that. It has to do exactly what you decided, and you should observe it happening too. There’s no compromise at all here, if it doesn’t do what you meant then it’s wrong.

If you find out it can’t be done then you have to change the strategy. Can’t deviate from the strategy, that would be wrong. Have to change it. (though, more likely if the strategy makes sense then there’s a way to implement it too)

And then if something isn’t behaving right then you start looking, probably somewhere in the middle of things, is this what you expect? If yes, look later, if no, look earlier.

I tried turning name into a list, and having each iteration of the list append an extra stack onto it. This didn’t fix anything. I’m not sure how else to alter my code to preserve the names of all three lists.

Doesn’t sound to me like you’ve established what’s actually wrong about the situation.

If you compare what you have with what you should have, then that pretty much outright says what you should change.

Surely a name should be a string? Why would a list be better? What’s wrong with string? It’s a stab in the dark, not going to help you.

turning name into a list and appending stack to it?
this does not make sense.
take a step back and ask yourself what should happen and what you have available to you.

You have a list of stacks.
Each stack has a name.
You have a first letter of a name.
You should select a stack by that first letter.
So you would look at each stack, and compare the first letter of its name to the letter that you have.

This is why I make a fuss about control and deciding what should happen.

All decisions are made based on that. Without that you have nothing, without that everything you do will be wrong.

Further, decisions around what should happen is entirely about every-day logic, so you can do that with very high confidence that it’s right.

And when you do have that very firm idea of what should happen, and sticking to it without compromise – everything becomes much easier. You have a reference to what everything should be.

I tried printing out a function call to get_input(), and whichever letter I select, I keep returning the left stack. Perhaps my previous theories about name are wrong, since if it was being totally overwritten, the stack returned should have been the right stack. By the way, when writing a function, what does it mean if it successfully prints out all the items (albeit only for one stack) and then on the next line it prints None? Did it return it or not?

Presumably you’re asking this:

def f():
    "do nothing"

print(f())  # None

If the return value is None, then you might ask, hey, what did I mean should be returned? If f is supposed to return 5, then, there should be return 5 somewhere in it.

Does it matter what happened? All that really matters is it didn’t do what you meant, so then you would investigate the part where you do that thing you meant should happen.

I figure if I know what happened it might be easier to fix. Besides, I’m curious. It seems strange to me it seems to simultaneously return and not return it. It’s Schrödinger’s function!

my f function doesn’t specify a return value. looking at its return value gives None

…not a whole lot else to say about that.

Yes, but my function does specify a return value, and does print, yet doesn’t return.

that doesn’t add up. re-do your observations. maybe you mean that you have a return statement, but do you ever execute it, did you observe that happening?

alternatively, maybe you’re printing None somewhere else, and the return value is actually something else.

… or maybe you specified that the return value is None, in which case finding None as the return value is entirely unsurprising

I tried changing the way I handle my return statement to:

        for i in range(len(stacks)):
        my_stack = stacks[i]
  return my_stack

This fixes the problem of the game telling me I’m wrong at every choice, but somehow bypasses all code before get_input() and traps me in an endless loop. :confused:

You can’t really say that it bypasses the loop if you’re stuck in the loop.

Anyway, refer to what should happen. It’s a trivial enough problem and you know where it is. Deciding what should happen will tell you what those loops should look like, because you necessarily have to decide in what manner to do repetition, whether it’s to repeat forever, or repeat until something happens or a fixed number of times.

given a letter and three stacks, which of those stacks has a name starting with that letter?

And, if you’re arbitrarily moving your return statement around, then, you’re probably ignoring what should happen, that’s the kind of compromise you can’t be doing. Look at a situation, compare to what it should be, and adjust accordingly. No random moving things around without reason.

So you’d loop forever.
When do you stop?
When you’ve found a letter matching one of the stacks.
Not only would you stop the loop, you’d exit the function as well.

YES!!! :joy: :tada: :confetti_ball: :fireworks: It finally accepts correct answers!!!

Hi, I’m wondering if you could show me the correct code for this Tower of Hanoi project. I’m also running into the same problem.

Please see the following guidance on setting up questions for the forums- FAQ: How to ask good questions.

Since this is a learning environment simply asking for code is inappropriate. Try to boil down your issues to a simple format and work through them and you’ll likely learn a great deal more by doing so. Folks on the forums are often willing to help nudge you in the right direction but you will be encouraged to explain what you’ve done and what you expected to happen at the least.