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
https://en.wikipedia.org/wiki/Infinite_monkey_theorem
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
So.
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.
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!!!
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.
Every time I run this code, I get a name error and I’m not sure why. Please Help.
from stack import Stack
print("\nLet's play Towers of Hanoi!!")
#Create the Stacks
stacks = []
left_stack = Stack('Left')
right_stack = Stack('Right')
middle_stack = Stack('Middle')
#Set up the Game
stacks.append(left_stack)
stacks.append(right_stack)
stacks.append(middle_stack)
print(stacks)
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 or equal than 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 {num_optimal_moves} moves'.format(num_optimal_moves=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('')
The error message reads:
“Traceback (most recent call last):
File “”, line 1, in
File “script.py”, line 37, in get_input
user_input = input(’’)
File “”, line 1, in
NameError: name ‘M’ is not defined”
Thank you
i was getting this error when i only typed python NOT python3 into the terminal