if, if ...
Wait a minute! Reeborg can make some decisions on his own. Didn't I tell you?
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:
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:
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!
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!