Towers Of Hanoi def Input Help

I’m having trouble getting my head round the last part of the function
if user_input.upper() in choices:
for i in range(len(stacks))
return stacks[i]

Would this not return the whole list of stacks, not the stack that the user chose from the input earlier in the function, or am I misunderstanding the function?

Any help with this would be much appriciated.

If return is met in the execution of a function then it signals the end of that function, it returns that value to the caller and control will switch back to the calling scope. Having return in a for loop seems like a mistake.

Double check the way this function is written, something seems off.

The steps in the project suggest the code be written, I’ve double checked it with the solution guide and it’s done in the way I’ve written in the post

Is it possible that stacks would be empty and it’s just an odd way of dealing with that fact (instead of using something clearer, like an if). So you iterate over the length but the length is zero there is no iteration,.

The description of return still applies though, you can only ever return once so no it doesn’t return every element, it either returns the 1st (index 0) element or it does not return at that point. It will not return multiple times.

Something about it still seems off to me, returning index 0 or nothing could be done in a much simpler fashion.

Personally I think using an index and an if statement would be a better choice here unless I’ve completely missed the point but I cannot see any reason to use a loop of that form :person_shrugging:.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.