Jed Rembold
August 29, 2025
Karel starts as shown and executes the code to the right. How many beepers are placed in the world at the conclusion of the program?
import karel
def main():
turn_left()
cool_thing()
neat_thing()
cool_thing()
def cool_thing():
move()
turn_left()
turn_left()
neat_thing()
def neat_thing():
put_beeper()
turn_left()
move()
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/true|||
else:
|||code to run if 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|||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 |||conditional test|||:
|||code to repeat as long as the answer to the test in yes|||
|||code to run once the answer to the test 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()
Whenever a loop ends, you just return to the same indentation level as when that loop began
For loops inside other loops then, this means that the “inner-most” loop runs from start to finish for every single step of the outer loop
What does the below chunk of code accomplish?
while front_is_clear():
move()
while not_facing_north():
turn_left()
turn_left()
put_beeper()Karel starts as shown to the right with 20 beepers in its bag. After executing the commands below, how many beepers are left in the bag upon the conclusion of the program?
while left_is_clear():
while front_is_clear():
move()
if no_beepers_present():
put_beeper()
turn_left()
Comments