Ralph L
Ralph L

Reputation: 53

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.

Upvotes: 1

Views: 5716

Answers (5)

shubham rao
shubham rao

Reputation: 1

The following is a sample snippet I wrote, please have a look.

def right():
    turn_left()
    turn_left()
    turn_left()
        
def mover():
    while wall_in_front()== True:
        turn_left()
        move()
        right()
    move()
    right()
    while front_is_clear():
        move()
    turn_left()        
                    
while at_goal() == False:
    if front_is_clear() == True:
        move()
    elif wall_in_front():
        mover()

Upvotes: 0

newbie
newbie

Reputation: 1

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

def jump():
    turn_left()
    move()
    turn_right()
    move()
    turn_right()
    move()
    turn_left()

while not at_goal():
    if wall_in_front():
        jump()
    else:
        move()

Upvotes: -1

Lucas Zanatta
Lucas Zanatta

Reputation: 1

I used this:

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

def action():
    if front_is_clear():
        move()
    elif wall_in_front():
        turn_left()
        while not right_is_clear():
            move()
        turn_right()
        move()
        turn_right()
        while not wall_in_front():
            move()
        turn_left()

while not at_goal():
    action()

Upvotes: -1

ken_nguyen
ken_nguyen

Reputation: 9

i think this my code optimize than your:

def follow_right_wall():
    if right_is_clear():
        turn_right()
        move()
    elif front_is_clear():
        move()
    else:
        turn_left()            
think(0)
while not at_goal():
    follow_right_wall()

Upvotes: 0

pepoluan
pepoluan

Reputation: 6808

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

Upvotes: 2

Related Questions