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

TASK IS:

Step 20

When the remainder of the move number divided by 3 is equal to 2, the movement is allowed between 'A' and 'B' (the source and the auxiliary rods).

Add an elif statement for that. Then, print the appropriate string if the condition is met.

…Which i did imo. tried to reset it a couple of times and start over but the code ist still rising an error. :frowning:

addition:
my code looks strange as the system is posting it here, on the lecture the elif is underneath the if statement like here:

if remainder == 1:
print(f’Move {i + 1} allowed between {source} and {target}‘)
elif remainder == 2:
print(f’Move {i + 1} allowed between {source} and{auxiliary}’)

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

# User Editable Region


# 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 (Macintosh; Intel Mac OS X 10_15_7) 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 20

1 Like

Hi @d.v.behrendt

Separate and from the curly brace.

Happy coding

1 Like

your elif should go to a new line

tried it, ithink it was even seperated before and only in my copied part it was wrong because the f string was going into the next line, but made sure again that its right on the code. unfortunately its still not passing yet. :frowning:

in the code it is under the if statement. i copied out a part of code in the addition at the end above to show how its looking like on the code. :frowning: in the quotation of my code it came out odd smh which happens every time on this thread.

please post your current code

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

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

those are not backticks, please use backticks so that your code is correctly formatted and indentetion is preserved

like this? messed up my indentation again when copying it in :( had to fix it manually

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

shouldn’t this be if reminder == 1:?

looks like this at first every time.

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

youre right, but i could swear that it was before as i had to exchange it for the remainder variable in a lecture before. :flushed:
had not noticed that it changed back jokes on me but i wasnt doing that. :flushed:
i changed it now again but the code still didnt 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}')      elif remainder = 2:
            print(f'Move {i + 1} allowed between {source} and {auxiliary}')

send the elif to a new line, that is also an issue in your code

the indentaion there is right on the code. it always gets messed up when i copy it in
looks like this in the code on lecture (manually fixed the paste here:

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

If you don’t have the going to new line when copying I would double check it’s actually there in the editor

ayway, are you sure this should be an assignment? this is the error raised before the code could be tested

no its meant to compare. unfortunately its not passing. :frowning: checked the elif multiple times and on code its where its meant to be. would send you a picture if i could :sob:

started it all over again and made a reset again. still not passing. thank you for your patience, but im really starting do get desperate and will take a break now. any help is still welcome since i really have no idea how to proceed from here :frowning: i found out that sending pictures actually is possible so i posted also a picture this time in aaddition to the code:

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

no, please don’t post a picture of your screen

also, go to a new line with that elif, for the eleventyeth time

the reason i posted the picture IS that you can see that it IS in a new line all the time as i said before. please look at the picture.

it’s on the same line as the print statement, it has to go on a new line