4-Functions ControlFlow
4-Functions ControlFlow
Adopted from Stanford Uni’s CS106ap course slides by Kylie Jue and Sonja Johnson-Yu
Today’s How do we teach Karel new things?
2. Karel functions
4. What’s next?
Remember Karel?
This is Karel’s world corners
(intersections)
walls
streets
beepers
(rows)
avenues
(columns)
What Karel can do
move()
turn_left()
put_beeper()
pick_beeper()
What Karel can do
move()
Makes Karel move forward one
square in the direction it is facing.
begin end
What Karel can do
turn_left()
Makes Karel turn left.
begin end
What Karel can do
put_beeper()
Makes Karel place a beeper on
the corner where it is currently
standing.
pick_beeper()
Makes Karel pick up a beeper
from the corner where it is
currently standing.
begin end
Karel Goes Home turning right
turn_left()
turn_left()
turn_left()
Karel Goes Home
move()
Karel Goes Home
turn_left()
turn_left()
turn_left()
Karel Goes Home
move()
Karel Goes Home
turn_left()
Karel Goes Home
move()
Karel Goes Home
pick_beeper()
How could I talk to Karel using Python
The structure of a program
import ...
def main():
...
if __name__ == '__main__':
...
The structure of a program
import ... Definition
computer program
def main(): A set of instructions for the
... computer – an “app.” These
instructions are also called
if __name__ == '__main__': lines of “source code.”
...
The structure of a program
import ... import modules (more on that
later in the quarter)
def main():
...
if __name__ == '__main__':
...
The structure of a program
import ...
if __name__ == '__main__':
...
The structure of a program
import ...
def main():
...
if __name__ == '__main__':
call your main function (you
...
won’t need to worry about
this)
The structure of a program
import ...
def main():
...
This is where we’ve
been writing our code!
Karel Goes Home (solution)
def main():
# Climb wall
turn_left()
move()
turn_left()
turn_left()
turn_left()
move()
turn_left()
turn_left() Turning
turn_left()
move() right is
# Go home
turn_left()
move()
tedious!
pick_beeper()
How do we teach Karel new commands?
How do we teach Karel new commands?
Functions!
Definition
function
A smaller part of your program that solves a
specific sub-problem
Functions
def main():
...
turn_right()
...
Turning right
def turn_right():
“““
Makes Karel turn right. Function definition
”””
turn_left()
What does the new Karel
turn_left() command do?
turn_left()
def main():
...
turn_right()
...
Turning right
def turn_right():
“““
Makes Karel turn right.
”””
turn_left()
turn_left()
turn_left()
def main():
...
turn_right() Function call
... Where you tell Karel to
actually do the command
Turning right
“Go find the function
def turn_right():
“““ definition, do the
Makes Karel turn right. commands inside,
”””
turn_left() and come back
turn_left()
turn_left() when done.”
def main():
... Function call
turn_right() Where you tell Karel to
... actually do the command
General structure of
functions
[whiteboard]
The structure of a function
def my_function_name():
“““
Write a good description
for your function here.
”””
# Put other Karel commands here
...
def caller_function():
my_function_name() # Function call
The structure of a function
Definition
function [header]
def my_function_name(): comment
“““ Comment that describes
Write a good description
for your function here. what a function does
””” using “““...”””
# Put other Karel commands here
...
def caller_function():
my_function_name() # Function call
The structure of a function
Definition
inline comments
def my_function_name(): Comments that describe a
“““
specific, complex block of
Write a good description
for your function here. code using #
”””
# Put other Karel commands here
...
def caller_function():
my_function_name() # Function call
The structure of a function
def my_function_name():
“““
Write a good description
for your function here.
”””
# Put other Karel commands here
Make sure lines inside
...
def caller_function():
each function are indented
my_function_name() # Function call
The structure of a function
Style note
def my_function_name(): indentation
“““ Each level of indentation
Write a good description should be four spaces.
for your function here.
”””
# Put other Karel commands here
Make sure lines inside each
...
def caller_function(): function are indented
my_function_name() # Function call
The structure of a function Style note
function names
def my_function_name(): Separate words with
“““
Write a good description underscores and use
for your function here. verbs
”””
# Put other Karel commands here
...
def caller_function():
my_function_name() # Function call
The structure of a function
def my_function_name():
“““
Write a good description
for your function here.
”””
# Put other Karel commands here
Note: The caller function
...
def caller_function(): doesn’t have to be main()!
my_function_name() # Function call
Think/Pair/Share:
Control flow!
Definition
control flow structures
Code features that affect the order, or flow, in
which the lines of code in a program happen
Conditionals
(if statements)
The if Statement
• Sequence structure: set of statements that execute in the order they appear
• Control structure: logical design that controls order in which set of statements
execute
• Decision structure: specific action(s) performed only if a condition exists
if ___________:
# Do something
If statements
if ___________:
# Do something
Condition
If statements
if ___________:
# Do something
Condition
(any boolean
expression)
If statements
if ___________:
# Do something
Condition
(any boolean
expression)Definition
boolean expression
A code snippet that evaluates to True or False
If statements
if ___________:
# Do something
Condition
True False
If statements
if ___________:
# Do something
Condition
True False
If statements
if ___________:
# Do something
Condition
True False
If statements
if ___________:
# Do something
If statements
if ___________:
# Do something
If statements
if ___________:
# Do something
If statements
Stop
The if Statement (cont’d.)
• Python syntax:
if condition:
Statement
Statement
• First line known as the if clause
• When the if statement executes, the condition is tested, and if it is true the block
statements are executed. otherwise, block statements are skipped
Boolean Expressions and Relational Operators
• Boolean expression: expression tested by if statement to determine if it is true
or false
• Example: a > b
• It is enough for one of the relationships to exist for the expression to be true
• == operator determines whether the two operands are equal to one another
• Example: if balance == 0
• Statements in inner block must be indented with respect to the outer block
General structure of
if statements
[whiteboard]
The structure of an if statement
if boolean expression:
if boolean expression:
● You can find all of these in the Karel Reference Guide on the course
website.
Let’s bring it back to Karel:
move_safely()
[demo]
Loops
(while loops)
Before we had if statements...
if ___________:
# Do something
Before we had if statements...
if ___________:
# Do something
• Time consuming
• Two parts:
• General format:
while condition:
statements
The while Loop: a Condition-Controlled Loop (cont’d.)
The while Loop: a Condition-Controlled Loop (cont’d.)
• In order for a loop to stop executing, something has to happen inside the loop
to make the condition false
• Iteration: one execution of the body of a loop
• while loop is known as a pretest loop
while ___________:
# Do something
While loops
while ___________:
# Do something
Condition
While loops
while ___________:
# Do something
Condition
True False
While loops
True False
While loops
while ___________:
# Do something
Condition
True False
While loops
while ___________:
# Do something
While loops
Drive forward
Let’s bring it back to Karel:
travel_row()
[demo]
General structure of
while loops
[whiteboard]
The structure of a while loop
● Potentially useful:
○ on_beeper()
○ not
While loop summary
(and misconceptions)
while loop summary
● All lines inside the while loop will run if the condition is true
while loop summary
● All lines inside the while loop will run if the condition is true
● Lines inside the while loop happen one at a time and in order
while loop summary
● All lines inside the while loop will run if the condition is true
● Lines inside the while loop happen one at a time and in order
● The condition is checked again only after all of the lines have run
while loop summary
● All lines inside the while loop will run if the condition is true
● Lines inside the while loop happen one at a time and in order
● The condition is checked again only after all of the lines have run
● All lines inside the while loop will run if the condition is true
● Lines inside the while loop happen one at a time and in order
● The condition is checked again only after all of the lines have run
● Forgetting not
Common bugs
● Forgetting not
● Forgetting not
● Forgetting not
● Forgetting not
• Something inside a while loop must eventually make the condition false
• Infinite loop: loop that does not have a way of stopping
● Forgetting not