Jed Rembold & Fred Agbo
January 22, 2024
As we mentioned before, Karel is a simple robot, and can really only do 4 potential actions
Command | Action |
---|---|
move() |
Moves Karel forward one corner in whatever direction they are facing |
turn_left() |
Rotates Karel 90 deg counter-clockwise |
pick_beeper() |
Picks up a beeper on the ground |
put_beeper() |
Places a beeper on the ground |
Our commands 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()
Suppose 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()
import karel
near the top of the program
karel.py
also needs to be in
the same directory as your programWhere you define what is in the bundle
Syntactically, it looks like:
def your_function_name():
any_commands_to_be_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()
def turn_right():
turn_left()
turn_left()
turn_left()
turn_right()
whenever we want to turn rightdef 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: Every 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 test answer is yes
else:
# Code to run if 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 test is true
# Carrying on with code that will always run
Another common use of predicate functions is in controlling a type of iterative function called a while loop
The structure of a while loop looks like:
while some_conditional_test:
# Code to repeat as long as the answer
# to the conditional_test is yes (true)
# Code to run once the answer is no
All of our predicate functions give yes-or-no answers though! So we can do something like
while front_is_clear():
move()
which will continually move Karel forward as long as there is not a wall in front of them!
def main():
"""
Main function to fill any number of
potholes at any location!
"""
while front_is_clear():
if right_is_clear():
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 deg to the right. """
turn_left()
turn_left()
turn_left()
def turn_around():
""" Turns Karel 180 deg around. """
turn_left()
turn_left()