Jed Rembold & Fred Agbo
January 24, 2024
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
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() |
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()
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 step of the outer loop
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()
Sometimes we know the number of times we want to loop
In these circumstances, the iterative statement called a for loop is best used
Syntax looks like:
for i in range(desired_count):
# statements to be repeated
desired_count
is an integer
indicating the number of times you want the loop to repeati
is a name that we will later make
more general, but for now you can always leave it as an
i
for
youimport karel
def main():
"""Draw a 4x4 square in the world."""
position()
draw_box()
def position():
"""Move to starting corner of box."""
move()
move()
turn_left()
move()
move()
turn_right()
def turn_right():
"""Turns Karel 90 deg to the right."""
turn_left()
turn_left()
turn_left()
def draw_box():
"""Draws a box with 4 equal sides in a CCW direction."""
for i in range(4):
draw_6_line()
turn_left()
def draw_6_line():
"""Draws a straight line of 6 beepers, if space."""
for i in range(5):
if no_beepers_present():
put_beeper()
if front_is_clear():
move()
if no_beepers_present(): # Last beeper to make 6
put_beeper()
move()
turn_left()
pick_beeper()
put_beeper()
front_is_clear()
Group code into bundles
def function_name():
# Code to be grouped
Run certain code blocks only if a condition is true
if condition_test:
# Code if answer yes
else:
# Code if answer no
while
loop: repeat code block as long
as condition is true
while condition_test:
# Code to repeat
for
loop: repeat set number of
times
for i in range(desired_count):
# Code to repeat
while no_beepers_present():
# if opening on right, we follow
if right_is_clear():
turn_right()
move()
else:
# go forward if possible or turn
if front_is_clear():
move()
else:
turn_left()