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

Tell us what’s happening:

Still getting an indentation error here.

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):
    

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/122.0.0.0 Safari/537.36

Challenge Information:

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

Hello @LowVel0city

That function requires the keyword pass if you are not going to do anything with it for now.

I’ve included the pass and am still receiving the below error even though I have pass indented properly below 'def make_allowed_move(rod1, rod2):

Your code has an indentation error. You may need to add pass on a new line to form a valid block of code.

Would you be able to post now the modified code?

Ay, caramba! Thank you for your attempt to post the code, but unfortunately, that only allows us to make a visual assessment, which will make it difficult to find hidden blank spaces.
Any chance you can post the code by copying and pasting inside (```) and (```) quotes.

Do you see that forward = False? From that on, it doesn’t look right.

I’m sorry, the only way I know how to post my code is making a whole new forum post. havaen’t figured out another way yet. I do not see the (‘’') you are referring to.

I have no idea how people comment and change the way their font looks to look like code like you did. Example below:

image

Hello!

I remember this task, so annoying.

Best reset the lesson.

  • Cut out every line from “forward = False” to “print(rods)” with ctrl+x.
  • Remove pass and paste the code in with ctrl-v.
  • Now select the code from “if not rods[target]” to “print(rods)” and move everything ONE level down, so if not/elif/if/else is on the same level as “forward = False”.

Then it should work.

I am still getting the indentation error. From your description after resetting, I wound up with the same code above. I am at a total loss for this one.

When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Screenshot 2024-03-14 162001

the forward and if not lines here (and those below) should be at the same indentation level as the print line above. They should not be indented further.

There was no reason to indent the 2nd line here.

I am still not passing.

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)

Hi @LowVel0city

You did not move the code into the correct function.

Please reset the step to restore the original code and try again.

Happy coding

You skipped points one and two.

This lesson will be the death of me. I am still very confused on what they are asking me. trying all these different indentations has now made me think I do not understand indentations at all, but felt like I fully understand their function until this lesson as I’ve had no indentation errors until this lesson.

I’ve done exactly what you stated and am still failing. Reset my web browser. Restarted computer. Still failing but seeing my exact code passing on other forums. I am at a loss

I know it is not the right way to learn, but I have even gone ahead a step in the forums to see the correct code. Have copied that code, and am still not passing. I am desperate to keep progressing.

def make_allowed_move(rod1, rod2):
    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
        if remainder == 1:
        print(f'Move {i + 1} allowed between {source} and {target}')
        

I have found the right code. I have no idea what makes it right, or why what I was doing was not passing. It looks the same to me. But I am too desperate to move on I can’t be hung up on this any longer, especially when I am not understanding the issue

Your code still had an indentation error here.

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