Jed Rembold
August 27, 2025
As we mentioned before, Karel is a simple robot, and can really only do 4 potential actions
A shared language is important, so we will use the below commands to communicate that we want Karel to do a specific action:
| Command | Action |
|---|---|
move() |
Moves Karel forward one intersection in whatever direction they are facing |
turn_left() |
Rotates Karel 90 deg counter-clockwise |
pick_beeper() |
Picks up a beeper on the intersection Karel is standing on |
put_beeper() |
Places a beeper at the intersection Karel is standing on |
Our instructions are just sequences of these actions
def main():
move()
turn_left()
turn_left()
turn_left()
move()
turn_left()
move()
turn_left()
turn_left()
turn_left()
move()
pick_beeper()
turn_left()
turn_left()
turn_left()
move()
move()
put_beeper()
import karel near the top of the program
karel.py also needs to be in
the same directory as your programSuppose you had Karel starting in the world shown to the right. If Karel then executed the commands shown to the far right, what intersection would they end up at?
move()
turn_left()
turn_left()
move()
move()
turn_left()
turn_left()
turn_left()
move()
Where you define what is in the bundle
Syntactically, it looks like:
def |||your_function_name|||():
|||any commands you want bundled|||Note that bundled commands need to be tabbed in!
Where you use the earlier defined bundle (the function)
Commonly referred to as calling the function
Use matched parentheses after the function name:
|||your_function_name|||()We can define a convenience function to turn right!
def turn_right():
turn_left()
turn_left()
turn_left()Then we can just type turn_right()
whenever we want to turn right
Internally, this is no different from what we had before (Karel still turns left 3 times), it is just easier to read and write
def main():
turn_left()
turn_left()
turn_left()
move()
turn_left()
move()
move()
turn_left()
turn_left()
turn_left()
move()
pick_beeper()
turn_left()
turn_left()
turn_left()
move()
move()
put_beeper()
We can define a convenience function to turn right!
def turn_right():
turn_left()
turn_left()
turn_left()Then we can just type turn_right()
whenever we want to turn right
Internally, this is no different from what we had before (Karel still turns left 3 times), it is just easier to read and write
def main():
turn_right()
move()
turn_left()
move()
move()
turn_right()
move()
pick_beeper()
turn_right()
move()
move()
put_beeper()
def turn_right():
turn_left()
turn_left()
turn_left()
def main():
move()
fill_pothole()
move()
move()
fill_pothole()
move()
def fill_pothole():
turn_right()
move()
put_beeper()
turn_around()
move()
turn_right()
def turn_right():
turn_left()
turn_left()
turn_left()
def turn_around():
turn_left()
turn_left()
Hashtag method: Everything following a hashtag (#) on the same line is ignored
# This is a short comment!
Triple quote method: Everything inside triple quotes (“““) is ignored
""" This is also a comment! """
def main():
"""
Main function to fill 2 potholes
at known locations.
"""
move()
fill_pothole()
move()
move()
fill_pothole()
move()
def fill_pothole():
"""
Fills a single pothole and returns
to where it started.
"""
turn_right()
move()
put_beeper() #assuming infinite beepers available
turn_around()
move()
turn_right()
def turn_right():
""" Turns Karel 90 to the right. """
turn_left()
turn_left()
turn_left()
def turn_around():
""" Convenience function to turn Karel 180 around. """
turn_left()
turn_left()
Potential questions you can ask Karel include:
front_is_clear() |
front_is_blocked() |
left_is_clear() |
left_is_blocked() |
right_is_clear() |
right_is_blocked() |
beepers_present() |
no_beepers_present() |
beepers_in_bag() |
no_beepers_in_bag() |
facing_north() |
not_facing_north() |
facing_south() |
not_facing_south() |
facing_east() |
not_facing_east() |
facing_west() |
not_facing_west() |
Predicate functions can be used to control a kind of “switch”: running one piece of code if the answer is yes and a different piece of code if the answer is no.
Commonly called if or if-else statements, they take on the syntax of:
if |||conditional_test|||:
|||code to run if the test answer is yes|||
else:
|||code to run if the test answer is no|||If you don’t want the code to do anything special if the answer is no, you can ignore the “else” part of the statement:
if |||conditional_test|||:
|||code to run if the test answer is yes/true|||
|||code that will always run, regardless of the test answer|||