Here's the cause of the problem we had at the end of part 1: we put down a beeper and, before we had the chance to move, tested to see if we were not next to a beeper. Since we were, we never go the chance to get in the while loop. Perhaps we can change the program to add a move() before we start the loop, as follows:
put_beeper() move() while not next_to_a_beeper(): if front_is_clear(): move() else: turn_left() turn_off()
Success! Try it!
Let's try the program we wrote on a slightly more complicated world, like the one below which you can easily reproduce.
The result is not exactly what we wanted: Reeborg takes a shortcut, and doesn't go all the way around.
The problem is that we assumed that Reeborg only had to move forward or turn left to go around the world; we never took into account situations where we would have wanted him to make a right turn. What Reeborg needs to do is first to check on his right to see if there is still a wall; if not, we have him make a right turn. Here's a modified program that attempts to do just that:
def turn_right(): repeat(turn_left, 3) put_beeper() move() while not next_to_a_beeper(): if right_is_clear(): turn_right() elif front_is_clear(): move() else: turn_left() turn_off()
Does it work? Read it carefully to decide for yourself. Then try it to confirm your opinion.