Introduction to Edward

Let's introduce you to our robot, Edward. He is here to make our learning experience more effective and engaging. Learning by interacting with Edward will make abstract concepts more tangible and easier to understand. We will instruct Edward to carry out some actions and since Edward is a Python-based robot, we’ll be learning Python programming along the way.

Press + to interact
Edward the Robot!
Edward the Robot!

Isn’t that cool? So let’s get to know our friend Edward better by interacting with him. As we progress through the lesson, we’ll get to know Edward through the functions it can perform.

move()

To instruct Edward to move to the green block, let’s add the functionality for him to move. Click the move() button twice in the widget below and see what happens.

It’s awesome, right? Edward can actually move! Let’s see what else we can make him do.

turn()

Let’s give Edward the ability to turn! Try using the turn() function to change his direction.

Can you make Edward turn toward the turn() button?

Hint: You might have to press the button twice.

turn() and move()

Now, to guide Edward to the green block, use the turn() and move() buttons.

Look at Edward go! He can turn in any direction and move around now.

Notice that whenever we click any of the command buttons, they appear inside the widget's command log. This is so that we can keep track of all the commands in the order that we execute them.

remove_trash()

Edward is an eco-friendly robot and wants to have a non-polluted, clean habitat. Let’s add the functionality for Edward to remove trash. With this new ability, he can help keep his world clean.

place_plant()

Let's introduce a new ability for Edward: placing plants! Use the place_plant() button to help him make his home more beautiful by planting trees.

Edward, perform all functions

Now, our task is to guide Edward to where the trash is so that he can remove it and place a plant instead.

Voila! Edward can now:

  • Turn in any direction

  • Move in that direction

  • Place a plant

  • Remove trash

We just instructed Edward to perform the above actions. This is, in fact, what programming is—instructing a computer (a machine) to perform some task.

Press + to interact

Functions in Python

We have a Pythonic term for each of the actions that Edward performed; it’s called a function. In programming, we use functions to perform specific actions or tasks that a computer, or in this case, Edward, can perform.

When we want Edward to perform a specific action, we use its corresponding function. To use any function in Python, we must make a call to it. This can be done by writing the name of the function, followed by a set of parentheses (). For example, the move() function tells Edward to move, the turn() function tells him to turn, and so on.

Interacting with Edward using Python

Just as we click the buttons to instruct Edward to perform some action, we can create functions in Python to define Edward’s actions in our code.

So Edward can move in all directions and turn to face any direction. He can plant trees to add greenery to his habitat and remove trash to keep it clean. Let's see the Python code and run it to do the same, but in code this time!

To see what we mean, copy the Python function calls from the command log of the widget above in the code editor below and then click the "Run" button.

Press + to interact
# Paste the function calls from the command log (of the above widget) here.

Congratulations, you just executed your first Python code successfully!

Python lets us write more than one line of code, and then executes those lines one by one. So Edward acts in sequential order as per each function call in the widget above and performs some actions (turn() and move() etc.) multiple times.

What's important to note is that we can reuse these functions to repeat the same tasks again, similar to how Edward repeated specific actions (like turning multiple times or moving a number of blocks ahead). This is what we mean by how functions can be called multiple times.

In your own words

“If you can’t explain it simply, you don’t understand it well enough.”
~Albert Einstein

Let’s check our understanding of the concept of functions that we’ve learned so far. In the notepad widget below, explain in your own words what you understand by functions and function calls. Also, write the syntax of a function called run.

Knowledge check on functions

Recap

In this lesson, we mapped Edward’s actions to a Python concept called functions. We wrote and ran our first Python code using functions. Let's add one more feature to Edward: the ability to communicate with us.