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

Tell us what’s happening:

I’m working on Step 31 of the Learn Recursion by Solving the Tower of Hanoi Puzzle challenge, but I’m stuck. I’ve tried multiple approaches, but the test still doesn’t pass. I understand that some code needs to be moved to a new function, but I’m unsure how much of it should be moved.

I reset the lesson to avoid confusion, so now I’m wondering: Should I move everything except the print() statement, or is there a specific part that needs to stay? :thinking:

If anyone has completed this step, I’d really appreciate some guidance. Thanks in advance!

Your code so far

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

# User Editable Region

def make_allowed_move(rod1, rod2):
    pass

def move(n, source, auxiliary, target):
    # display starting configuration
    print(rods)
    for i in range(number_of_moves):
        remainder = (i + 1) % 3
        if remainder == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
            forward = False
            if not rods[target]:
                forward = True
            elif rods[source] and rods[source][-1] < rods[target][-1]:
                forward = True
            if forward:
                print(f'Moving disk {rods[source][-1]} from {source} to {target}')
                rods[target].append(rods[source].pop())
            else:
                print(f'Moving disk {rods[target][-1]} from {target} to {source}')
                rods[source].append(rods[target].pop())

            # display our progress
            print(rods)

# User Editable Region

        elif remainder == 2:
            print(f'Move {i + 1} allowed between {source} and {auxiliary}')
        elif remainder == 0:
            print(f'Move {i + 1} allowed between {auxiliary} and {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/133.0.0.0 Safari/537.36

Challenge Information:

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

that’s what the instructions say

Move the code nested inside the first if statement (except the first print() call) to your new function. Pay close attention to the indentation. Don’t forget to remove the pass keyword.

So, I don’t move the for loop?

if remainder == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
            forward = False
            if not rods[target]:
                forward = True
            elif rods[source] and rods[source][-1] < rods[target][-1]:
                forward = True
            if forward:
                print(f'Moving disk {rods[source][-1]} from {source} to {target}')
                rods[target].append(rods[source].pop())
            else:
                print(f'Moving disk {rods[target][-1]} from {target} to {source}')
                rods[source].append(rods[target].pop())

            # display our progress
            print(rods)

And move all this part to new function?

is the for loop inside the first if statement?

No it is not, but even without it, a error shows:

Sorry, your code does not pass. You're getting there.

You should move the code nested inside the first if statement (except the first print() call) to your new function.

But did I?

def make_allowed_move(rod1, rod2):
    if remainder == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
            forward = False
            if not rods[target]:
                forward = True
            elif rods[source] and rods[source][-1] < rods[target][-1]:
                forward = True
            if forward:
                print(f'Moving disk {rods[source][-1]} from {source} to {target}')
                rods[target].append(rods[source].pop())
            else:
                print(f'Moving disk {rods[target][-1]} from {target} to {source}')
                rods[source].append(rods[target].pop())

            # display our progress
            print(rods)

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

inside the first if statement does not include the if statement itself. The first line nested inside the if statement, and after the first print() is forward = False.

1 Like
def make_allowed_move(rod1, rod2):
    
        if not rods[target]:
            forward = True
        elif rods[source] and rods[source][-1] < rods[target][-1]:
            forward = True
        if forward:
            print(f'Moving disk {rods[source][-1]} from {source} to {target}')
            rods[target].append(rods[source].pop())
        else:
            print(f'Moving disk {rods[target][-1]} from {target} to {source}')
            rods[source].append(rods[target].pop())

        # display our progress
        print(rods)

def move(n, source, auxiliary, target):
    # display starting configuration
    print(rods)
    for i in range(number_of_moves):
        remainder = (i + 1) % 3
        if remainder == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
            forward = False

Hmm Console giving error:

Sorry, your code does not pass. Keep trying.

You should move the code nested inside the first if statement (except the first print() call) to your new function.

and perview not:

{'A': [3, 2, 1], 'B': [], 'C': []}
Move 1 allowed between A and C
Move 2 allowed between A and B
Move 3 allowed between B and C
Move 4 allowed between A and C
Move 5 allowed between A and B
Move 6 allowed between B and C
Move 7 allowed between A and C

did you move everything that is inside the if statement (not counting the print)?

or, what is forward inside make_allowed_move?

def move(n, source, auxiliary, target):
    # display starting configuration
    print(rods)
    for i in range(number_of_moves):
        remainder = (i + 1) % 3
        if remainder == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
            forward = False

Only these are in old functions and for forward it is throwing error if i place it to new function.
And these in new function:

def make_allowed_move(rod1, rod2):
    
        if not rods[target]:
            forward = True
        elif rods[source] and rods[source][-1] < rods[target][-1]:
            forward = True
        if forward:
            print(f'Moving disk {rods[source][-1]} from {source} to {target}')
            rods[target].append(rods[source].pop())
        else:
            print(f'Moving disk {rods[target][-1]} from {target} to {source}')
            rods[source].append(rods[target].pop())

        # display our progress
        print(rods)

what about this line?
isn’t this inside the if and not the first print?

1 Like

Python position is harder than CSS centering! :laughing: I finally passed it—just had to fix the position of forward = False in new function. Thanks for the help!

fix the position? you had not moved it at all

Yeah, I just moved forward = False to the top of the new function and kept adjusting until it worked! :sweat_smile:

you may want to review how indentation in python work if that was confusing

It was little bit not too much as in other position not matter so moving to python it got me sometimes