I am trying to solve Hurdle 4 for challange from - 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.
3 Answers
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
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() 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() 2