Learn Recursion by Solving the Tower of Hanoi Puzzle - Step 39

Tell us what’s happening:

I know how to complete this step, but when I don’t comment out the last line, why does it result in an index error? Pardon me if this is an out of place question.

Your code so far

NUMBER_OF_DISKS = 4
number_of_moves = 2**NUMBER_OF_DISKS - 1
rods = {
    'A': list(range(NUMBER_OF_DISKS, 0, -1)),
    'B': [],
    'C': []
}

def make_allowed_move(rod1, rod2):    
    forward = False
    if not rods[rod2]:
        forward = True
    elif rods[rod1] and rods[rod1][-1] < rods[rod2][-1]:
        forward = True              
    if forward:
        print(f'Moving disk {rods[rod1][-1]} from {rod1} to {rod2}')
        rods[rod2].append(rods[rod1].pop())
    else:
        print(f'Moving disk {rods[rod2][-1]} from {rod2} to {rod1}')
        rods[rod1].append(rods[rod2].pop())
    
    # display our progress
    print(rods, '\n')

def move(n, source, auxiliary, target):
    # display starting configuration
    print(rods, '\n')
    for i in range(number_of_moves):
        remainder = (i + 1) % 3

# User Editable Region

        if remainder == 1:
            if n % 2 != 0:
                print(f'Move {i + 1} allowed between {source} and {target}')
                make_allowed_move(source, target)
            else:
               print(f'Move {i + 1} allowed between {source} and {auxiliary}')
               make_allowed_move(source, auxiliary) 

# User Editable Region

        elif remainder == 2:
            print(f'Move {i + 1} allowed between {source} and {auxiliary}')
            make_allowed_move(source, auxiliary)
        elif remainder == 0:
            print(f'Move {i + 1} allowed between {auxiliary} and {target}')
            make_allowed_move(auxiliary, target)
           
# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Learn Recursion by Solving the Tower of Hanoi Puzzle - Step 39

Welcome to the forum @dogscatsn663

If you look at the output, you can see that the execution stops at the third move because of an IndexError. This happens because the code is still incomplete and needs an else clause that you will be writing soon. To make it work, turn your make_allowed_move() call into a comment.

Happy coding

Thank you, I read that already, but what is the LOGIC of it not working? Why doesn’t it just continue (in an albeit, incorrect manner). Or is it simply because its incomplete? I may be reading too deeply into this.

The code is trying to do something impossible or illogical so it stops running. It’s a bug!

https://www.pythonforbeginners.com/basics/indexerror-in-python

You can print out your variables and try to follow them an see why it stops, that’s a good exercise.

You can also put your code into something like this to step through each line and see how it’s executed:
https://pythontutor.com/python-compiler.html#mode=edit

It will give you a lot more insight into what’s happening.

I’m sorry but from the start this problem is very poorly done… and in this last step, step 39 … as the case was in step 38 … NUMBER_OF_DISKS or n, former is the argument and latter is the parameter, it’s completely irrelevant in the execution since the function references number_of_moves = 2**NUMBER_OF_DISKS - 1 which is defined OUTSIDE of the function namespace … anyway, I firmly believe this is what’s causing major problems at least for me as you keep saying it will stop executing with IndexError if n is odd but that’s simply not true following state progression from step 1 to 37 … anyway, just some feedback

Thank you for helping make FCC better. Bugs and feedback can be reported as GitHub Issues. Whenever reporting a bug or giving feedback, please check first that there isn’t already an issue for it and provide as much detail as possible.