Karel the Robot Teaches Python!

Betul Senoglu
3 min readNov 8, 2020

--

Who is Karel ?

Karel is the robot which is able to perform some basic commands by us. He is performed through some of fundamental functions in order to solve some of problems in his own world.

His creator is Stanford graduate student named Rich Pattis. Rich Pattis decided that making the teaching process in any programming language should be in the simplest way. He achieved characterizing most programming language by Karel the Robot. Yes, you’ve heard it right! Karel is the way to understand Javascript, Java and Python fundamentals.

Here is the some visuals to see Karel and his world;

Image from https://compedu.stanford.edu/

What can Karel Do?

move()Commands Karel to move one step forward. Karel can respond this command unless there is a wall blocking the way.

turn_left()Commands Karel to rotate 90 degrees in the counterclockwise direction.

pick_beeper()Commands Karel to pick up one beeper and keep it in its beeper bag. Karel can also keeps infinite number of beepers.

put_beeper()Commands Karel to take a beeper from its bag and put it down on the current position. Karel does this leaving a beeper task if he already has one or more.

Note that; Karel is able to understand the for loops, if-else or while structures. Here are some of commands you may use with these structures.

frontIsClear() — frontIsBlocked()

leftIsClear() — leftIsBlocked()

rightIsClear() — rightIsBlocked()

beepersPresent() — noBeepersPresent()

beepersInBag() — noBeepersInBag()

facingNorth() — notFacingNorth()

facingEast() — notFacingEast()

facingSouth() — notFacingSouth()

facingWest() — notFacingWest()

You may get your karel with the instructions in here; https://pypi.org/project/karel-robot/

What can You Do with Karel?

  1. We can ask Karel to solve many problems.
  2. We can program Karel to do that.

Here is an example of Karel movement by some piece of Python code;

Image from https://compedu.stanford.edu/
def main():
move()
turn_left()
turn_left()
turn_left()
move()
put_beeper()
turn_left()
turn_left()
move()
turn_left()
turn_left()
turn_left()
move()

After a few researches for you guys, I found a very useful online IDE for Karel Programming. The IDE is being reached out here: https://codehs.com/editor/3187838

Let’s write Karel programs here and feed them to the simulator to watch them execute.

Image from https://codehs.com/editor/3187838

Solution:

from karel import *
def turn_right()
turn_left()
turn_left()
turn_left()
move() # go to (2, 1)
move() # go to (3, 1)
turn_left() # turn north (^)
move() # go to (3, 2)
move() # go to (3, 3)
turn_right() # turn east (>)
move() # go to (4, 3)
move() # go to (5, 3)
turn_right() # turn south (v)
move() # go to (5, 2)
move() # go to (5, 1)
turn_right() # turn west (<)
move() # go to (4, 1)
take_ball() # take the ball at (4, 1)

I hope, you guys had fun with this article! Explore Karel more here https://karel.csbridge.org/python/en/chapter1.html and have more fun!

--

--

No responses yet