Letting Edward Go Free

Bid farewell to Edward by automating its tasks.

Previously, we provided Edward with the capability to travel on a straight path until the path ended. It was accomplished by making a simple while loop with the stopping condition of a blockade and moving otherwise as shown in the widget below.

Python 3.10.4
while goal_not_achieved():
move()

Turning at a bend

The code we wrote worked fine. However, the paths we have to travel won't always be straight. There will be bumps, hurdles, and bends, and we need to prepare Edward for that.

Let’s see what happens if we use the same code for a path that has a bend before we can reach the destination.

Python 3.10.4
while goal_not_achieved():
move()

Oops! Edward got stuck at the blockage and never reached the final goal.

Fortunately, our friend Edward has some pretty decent cameras installed in his eyes and can see what lies ahead. Using the function front_is_blocked(), we can use these cameras to see if the path ahead is clear or not, and teach Edward to turn when required. This function returns True if the path ahead is blocked, and False otherwise.

canvasAnimation-image
1 / 7

We learned about conditionals a few lessons ago. Let's make some required checks and implement conditionals to teach our friend Edward what to do when the path ahead is blocked.

Study the code below and try to guess what it will do. Then, click the “Run” button to see the output.

Python 3.10.4
while goal_not_achieved():
if front_is_blocked():
turn()
else:
move()

Turning at another bend

Nice! Edward is now able to navigate on his own through that turn. But can he navigate perfectly when more than one turn is required? For example, run the widget below to see a pathway with more than one turn.

Python 3.10.4
# Use this space to write the code that automates Edward.

Now write the automation code and see if Edward is able to navigate through the second turn as well. You can check the solution if required.

Bidding farewell to Edward

This is our final exercise with Edward and the time to bid him farewell. Edward has been a great friend throughout this journey. Let's provide him with one last function as his farewell gift: trash_present(). This function will allow him to detect if there is any trash in the block that he is standing in. If trash is present, this function will return True, and False if the block is clean.

Edward’s final goal is to reach his destination. On his way, he will remove trash using the remove_trash() button, and put a plant in its place using the place_plant() function.

Python 3.10.4
# Use this space to write the code that automates Edward.