Suppose we want to have Reeborg keep walking until he finds a beeper, and then turn himself off. An example of a starting position is below:
One way to do this- in pseudocode- would be as follows:
If next to beeper,
... stop;
otherwise,
... keep moving.
Using what we have seen so far, we could translate this as:
if next_to_beeper(): turn_off() else: move()This is only part of the solution as we have not told Reeborg to keep on repeating. Let's ignore this for now. Instead, consider the equivalent pseudocode formulation:
if not next_to_beeper(): move() else: turn_off()
To transform this into a complete program, using what we have seen so far, we could need to define an instruction, and then have it repeated often enough to ensure that Reeborg can complete its task.
Use the new Python keyword not and rewrite your jumping hurdle program.
It is spring time. Reeborg's father has seeded the garden for the fall harvest. In some places, two seeds have sprouted whereas in others none have. A typical situation is shown below (file: harvest4.wld).
Help Reeborg weed the garden, so that there are no places where two carrots (beepers) are, and reseed so that there is no places with no carrots. The final situation should look as follows:
Here's a suggestion for part of the code:
# introducing vocabulary related to the problem next_to_a_carrot = next_to_a_beeper plant_carrot = put_beeper pick_carrot = pick_beeper def one_carrot_only(): if not next_to_a_carrot(): plant_carrot() # replace missing seed else: pick_carrot() if not next_to_a_carrot(): # oops! plant_carrot() # we had removed the only one
Note that, in reality, it is not a good idea to remove seedlings and replant them right away!