Karel’s world – week 5

“For what I’ve seen of programmers, they’re either good at all languages or good at none.
The guy who’s a good C programmer will be good at Erlang — it’s an incredibly good predictor.
I’ve seen exceptions to that but the mental skills necessary to be good at one language seem to cover to other languages.”

— Joe Armstrong, designer of the Erlang computer language

Handout: 3_Intro_1_karel

Reeborg, our Karel simulator, and Python, its language

The author of our robot simulator, Reeborg, is the Canadian physicist André Roberge (see our Hall of Fame) who has paid particular attention to our class at PCR; many of the features that we find in Reeborg today are the result of feedback from our classes at PCR. He would welcome a note from a PCR student thanking him from his efforts (andre.roberge@gmail.com).

The simulator runs in Python, the most popular computer language in the world, used extensively in academia, research and industry, and often used as a first language because of its clarity.

Python's popularity in 2015: 31.2%

Python’s popularity in 2015: 31.2%

The Reeborg GUI

Let’s start it up and solve a mission: click here.

First, identify the different parts of the interface:

Karel’s world

  • this is the grid where Karel lives
  • the cells may be empty of contain an object:
    • one or more tokens that Karel can pick up
    • an ’empty’ token where we should tokens to end the mission correctly
    • Karel’s destination, or ‘home’ square
  • there can be walls between cells
  • On the top-left area, we can see how many tokens Karel has in its bag

karels world

code editor

  • The code editor has a ‘Python code’ tab and a ‘library’ tab
  • We write our programs in the ‘Python code’ area only
  • The ‘library’ area will have functions that our program can use
  • Our programs should not be longer than 30 lines


Control buttons

On the top-left area, from left to right, we these buttons:

  • play button: runs the program
  • step button: runs only the next command
  • pause button: stops the program temporarily
  • stop button: stops the program without reseting the world
  • rewind button: stops the program and resets the world


Solving a mission

We are now going to solve a mission based on the world that we saw before. Karel needs to pick up the yellow token, put it in the basket and then go home. So let’s get started.


show it to me

A comment is text that is not part of the program; it has information for the programmer, not for the computer, i.e., it is like a margin note. In Python, any text that follows a hashmark # is a comment.

Let’s start the program with the following comment:

Now, when we run the program (i.e., when we press the ‘play’ button), the program will run and fail because, although the program is valid, it does not achieve any of the goals of the mission, i.e., there is no token in the basket and there is no robot on the green square.


System commands

show it to me

The following are a few functions that will appear often in our missions.

  • sound( True/False ): turns the sound effects on and off
  • think(n): sets a delay between commands, in milliseconds. Delays shorter or equal to 250 ms turn off the sound effects.
  • say(message): prints a message in a pop-up box.
  • pause(): pauses the program until we press the play or step button

Let’s add them in our program:

With these commands we are:

  1. turning the sounds on
  2. setting the delay between commands to 1 second
  3. assigning a string to the variable msg
  4. printing the message to the screen with the function say()
  5. pausing the program


Built-in commands

show it to me

An actuation command (or function) allows a robot to act. Karel has 4 built-in actuation functions:

Let’s use these commands to accomplish the first task, i.e., to put the token in the basket, adding the following code:

When we run the program now, it still fails but indicates that the tokens are now in their correct final location.


A mission fails if…
…we crash Karel against a wall
…we ask him to take a token while standing on an empty cell
…we ask him to put a token but his bag of tokens is empty



show it to me

Repeat() repeats a single command multiple times:

For example, Karel has placed the token in the basket; now he needs to turn around to go home. To turn around we can tell him to turn_left() twice or, better, use the repeat() command to tell him to do turn_left() 2 times.

In summary, instead of writing this:

we are going to write this:

Both pieces of code do the same thing but the second version is better because does the same thing in a more clear way. Again:

the 2nd version is better because it is clearer than the first one, not because it is shorter.


The library

show it to me

Although Karel only knows 4 commands when we turn him on, we can teach him new commands that are based on the 4 commands that he already knows. We have provided you with 3 additional commands that you can find in the library:

  • left()
  • right()
  • turn_around()

In particular, turn_around() does exactly what we just did ourselves: turns the robot around by using repeat(). Thus, instead of repeating the code, we just use the command already written in the library. We tell Python that we want to use the commands in the library placing the following line at the top of our program:

which means ‘allow my program to use everything in my_lib‘.

Hence, we can modify our program to use the library like this:


3rd grade – Almost there

Work it out

What? The training mission is incomplete: Karel needs to go to the home square. Please, finish the mission and take Karel home for a well-deserved rest after all this token taking-and-putting.

almost there

Answer: Show it to me

almost there solution

4th grade – A dozen chicks

work it out

Ahh!.. the chicks got out of the pen. Quick.. return them to the pen and then take Karel home.

Your main program must be:


Answer: show it to me



..and after.

..and after.

5th / 6th grade

There are no 5th or 6th grade mission this week because we covered a lot of material. If you were able to do the 4th level mission you are perfectly on pace. Congratulations.