In this multi-part lesson, we are going to write a complete program to solve problems of increasing complexity.
Let us start by considering a simple problem: having Reeborg go around his world once, assuming there is no obstacle on the way. We have done this before, when we introduced the front_is_clear() test. Here's the outline of a solution which supposes that Reeborg carries at least one beeper at the beginning:
1. Put down a beeper to mark the starting point. 2. Move forward until facing a wall. 3. Turn left when facing a wall. 4. Repeat steps 2. and 3. until we find the beeper we had put down. 5. Turn off when finding the beeper.
The key step is 4, where we have a repeating instruction with a test. This instruction can be written as:
while not next_to_a_beeper():
following which should be steps 2. and 3. Let's now translate the entire solution in proper code:
put_beeper() while not next_to_a_beeper(): if front_is_clear(): move() else: turn_left() turn_off()
Take the time to think about what the above program instructs Reeborg to do, if the starting position is the one illustrated below:
We did not get the desired result, which is indicated below. Did you figure out why? If not, go back and think about it.