Search code examples
pythonwhile-loop

Python while loop and Reeborg's World Challenge


I am trying to solve Hurdle 4 for challange from https://reeborg.ca/reeborg.html - Using Python, mainly while loops.

While below code works perfect for the challange:

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    
def pass_the_wall():
    turn_left()
    while wall_on_right():
        move()
    turn_right()
    move()
    turn_right()
    while front_is_clear():
        move()
    turn_left()      
    
while not at_goal():
    if wall_in_front():
        pass_the_wall()
    else:
        move()

I have written something like this:

def turn_right():
    turn_left()
    turn_left()
    turn_left()
    

def pass_the_wall():
    if front_is_clear():
        move()
    if wall_in_front():
        turn_left()
        while wall_on_right():
            move()
            while right_is_clear():
                turn_right()
                move()
                turn_right()
                move()
            while wall_in_front():
                turn_left()
                
while not at_goal():
    if wall_in_front():
        pass_the_wall()
    else:
        move()  

It can follow the path, but the Reeborg does not stop as loop is infinite.

I have a feeling, that it may work this way, but I've lost logic here and need some fress outlook.

Question is where I have made a mistake? Is there a way to not change my code and only add some lines, that will stop the loop.

I tried continue and break - not working here.

Thank you for help.


Solution

  • You need to add

    if at_goal():
        done()
    

    Right after one of your move()s.

    Which one? I'll leave it to you to find out 😉

    Edit: Okay, after TWO of your move()s