Managing Karel

To see what programming looks like, we will introduce you to Karel. Karel is an animated robot that moves along a maze-like table by following our instructions in the form of a program. Through managing Karel, we will adopt a logic that is very important for writing any program, and we can also have some fun along the way.

The idea of learning programming through controlling a robot dates back to the 1970s, when Richard E. Pattis, as a graduate student at Stanford University, created the first such environment with a programming language specially designed for this purpose. The language, like the robot, was named Karel, after Karel Čapek, the Czech writer who first started using the word robot. Patis’s book Karel The Robot: A Gentle Introduction to the Art of Programming was published in 1981 and quickly became the best-selling introductory book in programming courses.

To manage Karel we can use these functions:

By using these five functions, we state what we want Karel to do, so we will call them “commands for Karel”, or “statements”. Karel understands five more different functions, which we will see soon. In addition to these commands directly addressed to Karel, we can also use all the commands of the Python programming language, which we will learn during the course.

Let’s look at the examples of how the above-mentioned commands can be used to guide Karel through his world:

Examples

Move one square forward and take the ball

Write a program to move Karel to the field (2, 1) and make him pick up the ball.

This program consists of only two commands. The first one tells Karel to move one field forward, and the other one to take the ball.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_two_squares_one_ball)

Including karel library

The functions we use to manage Karel are defined in the library karel. Therefore, at the beginning of each program, we should tell the computer (more precisely, the program that executes our program) to first attach the definitions of these functions for managing Karel. This is achieved by the first line of the program: from karel import *. Every program dealing with Karel that we write should start this way.

Keep in mind that karel library can only be used in this environment for now. You can run your other programs in other ways, but we will remind you of that when the time is right.

There may be more than one ball on one square, and our task may be to tell Karel to take several, or all of them.

Move one square forward and take three balls

Write a program that tells Karel to move to the square (2, 1) and pick up three of the five balls that are there.

In this program, after the command move(), the command pick_ball() should be written three times, since we need Karel to pick up three balls. Pay attention to the number that appears on the ball. It shows how many balls there are on that square. In addition to that, the number to the left of Karel’s head (which you might have noticed in the previous example as well) tells how many balls Karel has with him.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_two_squares_five_balls)

Next task is similar, but somewhat more difficult.

Get to the ball and take it

Write a program that tells Karel to come to the field (4, 1) and pick up the ball.

The task does not essentially differ from the previous one. It is still necessary to bring Karel to the target square and tell him to take the ball. The difference is that now the path to the target square is longer, and so is our program:

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_take_ball_on_square_4_1)

Reading this program, it’s becoming hard to follow which command brings Karel to which square. This is not the case just with beginners, it’s difficult for anyone because every move() statement looks the same. To help both ourselves and yourself, after each command we have added the # sign and some text that helps us to follow where we have brought Karel.

Comments

Part of any Python program from the # character to the end of the line is called a comment. Comments do not affect the execution of the program, the program does the same thing with or without them. Comments are intended only for people who read and write programs, to help them better understand these programs and to handle them more easily.

When thinking about writing comments in our programs, we should write them for both ourselves and other people who will read our programs. On the other hand, the comments that other people write in their programs will help us understand their programs.

There are no precise rules for writing comments. In your comments, you should write down whatever you believe helps others (and yourself) better understand your program.

Pick up all the balls

In this example, the balls are on different squares and we need to bring Karel to each of these balls.

Write a program to tell Karel to pick up all four balls.

We can choose the path for Karel in many ways, but the shorter the path we choose, the shorter (and faster) our program will be. So, for instance, we could first take the ball at square (5, 2), then the two balls at (5, 5) and finally the ball at (4, 4).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_collect_three_balls)

Grouping commands

Since this program is even longer than the previous one, in order to make it easier to navigate in the program and track the Karel’s position, we made groups of commands that make up one stage of the journey and put each group into one line of the program. At the end of each line, there is a comment that explains the group of commands in that line.

Note that, when writing a program this way, the ; character must be written between the commands (after the last command in line, semicolon is not needed).

Commands can be grouped differently, for example by separating a group of commands (written one below the other) from the next group with an empty line. This way of grouping is used much more commonly, since commands are usually not as short as these for managing Karel. Here’s how it would look:

from karel import *

# go to square (3, 1) and turn north"
move()
move()
turn_left()

# go to square (3, 3) and turn east
move()
move()
turn_right()

# go to square (5, 3) and turn south
move()
move()
turn_right()

# come to square (5, 2) and take the ball
move()
pick_ball()

# turn north
turn_left()
turn_left()

# come to square (5, 5)
move()
move()
move()

# take two balls
pick_ball()
pick_ball()

# go to square (4, 4)
turn_left()
move()
turn_left()
move()

# take the last ball at (4, 4)
pick_ball()

Karel can also drop balls at squares. Here’s how he does it.

Move the ball

Write a program which makes Karel move the ball to the square (2, 2) (note that at the beginning Karel is not oriented properly).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_move_ball_in_2x2)

Errors in execution

Please note that Karel can not execute any command at any time. More specifically, Karel can not go forward if he is in front of a wall, he can not take a ball where there isn’t one, and he can not drop a ball if he has no balls with him.

Try deleting the first command turn_left() in the previous program, and then run the program to see what happens.

When the program executing our program comes to a command that can not be executed, the execution of our program is interrupted and we receive an error message. Such messages are normal and we will see them whenever Karel is unable to do what we told him, or when our statement is unclear (more precisely, when it is not written properly). In such situations, we should try to understand what the problem is, to fix the program and restart it.

Below are some tasks for independent work. With each task, a solution is offered, which you can see by clicking on the “solution” button. You can copy the displayed solution to the area for your work and try it by running the program. Note that your solution may differ from ours and still be quite ok.

Tasks for exercise

Come to square (3, 3)

In this task there are no balls, you only need to bring Karel to the square (3, 3).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_task_go_to_3_3)

Pick up the balls

Write a program based on which Karel will pick up the balls.

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_task_collect_balls_in_2x2)

Zig-zag

Karel should reach the square (5, 1).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_task_stairs_fixed)

Forward, then left, then again

Karel should get to the square (2, 3).

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_intro_task_spiral_left_fixed)