If only Reeborg could decide on his own ...

If, if, if ...

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


1. First decisions

Well, to tell the truth, Reeborg does need some help to decide: you have to give him some choices to decide between. For example, when Reeborg is next to a beeper, you can give him some choices as to what to do; for example, you can ask him to pick it up as follows

if next_to_a_beeper():
    pick_beeper()

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

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:

move_pick_start

and we want the final position to be:

move_pick_start

So, we want to ask Reeborg to:

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 next_to_a_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 chapter; 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.

harvest start

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

previous Avoiding repetitions, again! - home - Listen to me ... or else ....next
../images/SourceForge.net Logo