## 12. If only Reeborg could decide on his own ...

if, if ...

Wait a minute! Reeborg can decide make some decisions on his own. Didn't I tell you?

### First decisions

Well, to tell the truth, Reeborg does need some help to decide: you have to give him some choice as to what to decide. For example, when Reeborg is right next to a beeper, so that he appears to be "on" a beeper on the screen, you can give him some choices as to what to do: you could ask him to pick it up as follows

    if on_beeper():
        pick_beeper()

Let's look at the meaning of the above code:

- The python keyword if tells Reeborg that some condition, whose value is `True` or `False`, is going to follow;
- `on_beeper()` is a condition (or test) which is `True` if Reeborg is next to a beeper (at the same apparent position on the screen) and `False` otherwise;
  [note that this test was called `next_to_a_beeper()` in previous versions of rur-ple.]
- The colon (:) precedes the series of instructions that Reeborg must follow if the condition is `True`;
- The series of instructions to follow in that case is indented, just like we had in the case of definitions.

This explanation may seem complicated when you read it, but it's actually quite simple to use an if statement. Let's look at it in a simple example. Suppose we want Reeborg to take 9 steps, picking up any beepers that are there along the way. (We suppose that there can be at most one beeper at a given spot.) For example, the starting position might be like the following:

and we want the final position to be:

So, we want to ask Reeborg to:

- Take a step forward;
- Check to see if there is a beeper;
- Pick up the beeper if there is one; otherwise ignore and keep going;

repeating the above steps 9 times. Remember that if we ask Reeborg to pick up a beeper where there is none, he will complain and turn himself off. Here is how we can do this:

    def move_and_pick():
        move()
        if on_beeper():
            pick_beeper()

    repeat(move_and_pick, 9)
    turn_off()

Try it!

### Harvest time again!

It's harvest time, yet again! However, this time not all seeds have sprouted and some carrots are missing. Have Reeborg pick up all the carrots (represented by beepers) in this garden. The world file is harvest3.wld. Look back at the second last harvesting exercise that you did last time; chances are, all that you need to do is change the instruction `harvest_one_row()` so that it looks similar to the above `move_and_pick()` instruction; however, it is possible that you will need one more change. [For example, in my solution to the harvesting problem, I had defined a `pick_and_move()` instruction which needed some minor change as well.]

Note that your new program should also work as is with the world file harvest1.wld that we had used before. Try it!

Avoiding repetitions, again! - - Listen to me ... or else ....