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

I get this message

Your code raised an error before any tests could run. Please fix it and try again.

This challenge is like the fridge mechanic who charged the owner of a fridge $90 dollars for fixing the fridge. The owner shouted “$90 dollars? But you only turned a knob and the fridge started working again” Yes replied the mechanic. “For turning the knob I charged you only $5 dollars, but for knowing which knob to turn I charged $85 dollars”
If I knew what to fix it would help me to fix the error. Please help me to know what the error is. Thanks

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):
    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
        elif remainder == 2:

# User Editable Region

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

Challenge Information:

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

Hey Zack, the problem is that you accidentally deleted the if statement from the move function. Python sees that there is an elif and since that statement does not follow an if, it gives you an error. Try to reset the code and try again. Anyway the part you pasted in the make_allowed_move function should be fine.

@Dario_DC thanks for your hint. I have tried it and the code does not pass. Here is my updated code

NUMBER_OF_DISKS = 3
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[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)
    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}')

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

# initiate call from source A to target C with auxiliary B
move(NUMBER_OF_DISKS, 'A', 'B', 'C')

Please help. I have continued to edit, re-edit, reset and re paste my code. Every time I get the same error message Your code raised an error before any tests could run. Please fix it and try again.

Maybe in cases like this the Check Your Code (Ctrl + Enter) button could take a cue from Visual Studio, Visual Studio Code, Atom, PyCharm and Sublime etc. They give a traceback and point to the line of code where the error occurs. That way maybe people like me would become Better Bug Fixers

Haa!!! Now I have changed some indentation and it no longer asks me to fix the error. Okay. So far so good. Now it is asking me to move the code nested inside the first if statement (except the first print() call) to your new function.

Consternation!!! There are so many if statements that I can see inside my new function. the make_allowed_move() function Its getting close to midnight and I have been on this Step 31 all day. I wont give up.

1 Like

at this time it is not possible to give a clear error traceback for how the python code is executed in the browser, developers are working on it!

if you need more help I suggest you post your updated code

You should absolutely copy this code into an editor of your choice to experiment with it and see errors. I throw python code into a Google Colab document all the time to quickly test it. https://colab.research.google.com/

There are situations though where the code you will be writing in the fCC challenges is incomplete and will only be correct at a later step. This would generate errors in an editor, but be correct on fCC, just FIY

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