Skip to main content

Programming Karel

Karel Reader

Chapter 2: Programming Karel

The simplest style of Karel program uses text to specify a sequence of built-in commands that should be executed when the program is run. Consider the simple Karel program below. The text on the left is the program. The state of Karel's world is shown on the right:

# File: FirstKarel.py
# -----------------------------
# The FirstKarel program defines a "main"
# function with three commands. These
   commands cause
# Karel to move forward one block, pick up
   a beeper
# and then move ahead to the next corner.
from karel.stanfordkarel import *
def main():
move()
pick_beeper()
move()

Press the "Run" button to execute the program. Programs are typically written in a special application called an Integrated Development Enviroment (IDE) and most Karel programs are written in an IDE called PyCharm. Like an IDE, this reader has the ability to execute programs in order to help you see how things work as you learn.

The program is composed of several parts. The first part consists of the following lines:

These lines are an example of a comment, which is simply text designed to explain the operation of the program to human readers. Comments in both Karel and Python begin with the characters # and include the rest of the line. In a simple program, extensive comments may seem silly because the effect of the program is obvious, but they are extremely important as a means of documenting the design of larger, more complex programs. The second part of the program is the line:

from karel.stanford import *

This line requests the inclusion of all definitions from the karel.stanfordlibrary. This library contains the basic definitions necessary for writing Karel programs, such as the definitions of the standard operations move() and pick_beeper(). Because you always need access to these operations, every Karel program you write will include this import command before you write the actual program.

The final part of the Karel program consists of the following function definition:

   def main():
      move()
      pick_beeper()
      move()

These lines represent the definition of a new function, which specifies the sequence of steps necessary to respond to a command. As in the case of the FirstKarel program itself, the function definition consists of two parts that can be considered separately: The first line constitutes the function header and the indented code following is the function body. If you ignore the body for now, the function definition looks like this:

   def main():
body of the function definition

The first word in the function header, def, is part of Python's syntactic structure. It says that you are creating a new function. The next word on the header line specifies the name of the new function, which in this case is main. Defining a function means that Karel can now respond to a new command with that name. The main command plays a special role in a Karel program. When you start a Karel program it creates a new Karel instance, adds that Karel to a world that you specify, and then issues the main command. The effect of running the program is defined by the body of the main function, which is a sequence of commands that the robot will execute in order. For example, the body of the main function for the FirstKarel program is:

   move()
   pick_beeper()
   move()

Thus, if the initial state of the world matches the example given in Chapter 1, Karel first moves forward into the corner containing the beeper, picks up that beeper, and finally moves forward to the corner just before the wall, as shown in the following before-and-after diagram:

Before:
After:

Solving a more interesting problem

The FirstKarel program defined above doesn’t do very much as yet. Let’s try to make it a little more interesting. Suppose that the goal is not simply to get Karel to pick up the beeper but to move the beeper from its initial position on 2nd column and 1st row to the center of a ledge. Thus, your next assignment is to define a new Karel program that accomplishes the task illustrated in this diagram:

Before:
After:

The first three commands in the new program—the ones that move forward, pick up the beeper, and then move up to the ledge—are the same as before:

   move()
   pick_beeper()
   move()

From here, the next step is to turn left to begin climbing the ledge. That operation is easy, because Karel has a turnLeft command in its standard repertoire. Executing a turnLeft command at the end of the preceding sequence of commands leaves Karel facing north on the corner of 1st row and 3rd column. If Karel then executes a move command, it will move north to reach the following position:

From here, the next thing you need to do is get Karel to turn right so that it is again facing east. While this operation is conceptually just as easy as getting Karel to turn left, there is a slight problem: Karel’s language includes a turnLeft command, but no turnRightcommand. It’s as if you bought the economy model and have now discovered that it is missing some important features.

At this point, you have your first opportunity to begin thinking like a programmer. You have one set of commands, but not exactly the set you need. What can you do? Can you accomplish the effect of a turnRight command using only the capabilities you have? The answer, of course, is yes. You can accomplish the effect of turning right by turning left three times. After three left turns, Karel will be facing in the desired direction. From here, all you need to do is program Karel to move over to the center of the ledge, drop the beeper and then move forward to the final position. Here is a complete implementation of a StepUp program that accomplishes the entire task:

# File: FirstKarel.py
# -----------------------------
# The FirstKarel program defines a "main"
# function with commands that cause Karel to pick up
# a beeper and place it on a ledge.
from karel.stanfordkarel import *
def main():
move()
pick_beeper()
move()
turn_left()
move()
turn_left()
turn_left()
turn_left()
move()
move()
put_beeper()
move()
  • Turning off airplane mode
  • Turning on mobile data or Wi-Fi
  • Checking the signal in your area
ERR_INTERNET_DISCONNECTED

Comments

Popular posts from this blog

GTBank swift code and Foreign Currency Transfers details

We take your privacy seriously and only process your personal information to make your banking experience better. In accordance with NDPR, GDPR and other applicable regulations, continuing to use this platform indicates your consent to the processing of your personal data by Guaranty Trust Bank Plc., its subsidiaries and partners as detailed in our Privacy Policy Got it Home Business Banking International Trade Foreign Currency Transfers Foreign Currency Transfers Foreign Currency Transfers (GTMT) The GTBank International Money Transfer (GTMT) is designed to enable both GTBank account holders and non account holders send and receive funds to and from any GTBank subsidiary in West Africa. In line with a directive by the CBN, recipients of funds through GTMT in Nigeria must either be GTBank account holders or must be identified by a GTBank account holder. For Transfer Of USD ($) Into Domiciliary Account Through CitiBank New York: Correspondent Bank: CitiBank, New

OGUN STATE POLICE EMERGENCY NUMBERS

OGUN STATE POLICE EMERGENCY NUMBERS Below are the telephone numbers of area commanders, DPOS and Tactical team leaders of Ogun State Police Command.  Any of them can be reached in case of any distress. 1. O/C SARS 07065386591 2. O/C X SQAUD 08081774908 3. O/C S.I.B 08033173756 4. O/C E.O.D 08033447726 5. O/C ANTI-KIDNAPPING 08060374010 6. O/C HIGHWAY 08033208985 7. COMMANDER IGP 08035909911 8. O/C Q.R.S 07038649772 9. O/C MONITORING 08035249181 10. O/C SAFER HIGHWAY 07087253381 11. O/C IGP X SQAUD 08023529332 12. PPRO 08034241238 ABEOKUTA AREA COMMAND 1. ACPOL METRO 08023253470 2. DPO ADIGBE 08033448310 3. DPO IMALA 08030756843 4. DPO LAFENWA 08035141239 5. DPO ILUPEJU 08034610577 6. DPO KEMTA 08030642903 ADATAN AREA COMMAND 1. ACPOL ADATAN 08030742737 2. DPO ADATAN 08035518131 3. DPO ODEDA 08057150916 4. DPO OBANTOKO 08123824273 5. DPO OKE-ITOKU 08037767562 6. DPO ENUGADA 08037169917 7. DPO FUNAAB 08066634646 8. DPO AREGBE 08036041125 SAGAMU AREA COMMAND 1. ACPOL SAGAMU 08

RCCG SPECIAL THANKSGIVING SERVICE MAY 2020

Topic: Thank God for Calvary Text: Revelation 1:11-18 The greatest battle ever fought since the creation of man was fought at Calvary. It was a battle between the forces of darkness and Light. It seemed as if darkness won, but 3 days later, Light shone. What We Should Thank God For: 1. For the Blood He shed on Calvary. Through this Blood, our sins have been washed away. 1 John 1:7 The Blood is still giving us victory over satan. Revelation 12:10-11 The Blood also gives us access to God. Hebrews 10:19-22 Everyone of us now have access to the Holiest by virtue of the Blood. 2. The Healing Stripes of Jesus. 1 Peter 2:24, Isaiah 53:1-5 Jesus took a stripe for diseases yet undiscovered in addition to the 39 He took for categories of diseases already existing. 3. The Name of Jesus. Philippians 2:5-11, John 14:14 The Name has given us access to God's provision and deliverance. John 16:24 4. Victory over death.  Since Calvary, death has lost its sting. 1 Corinthians 15:54-57, Hebrews 2:14-