Week 2Fancy Face, Conditional
Execution, Recursive Tree
Computer Science I
Scott C Johnson
Fall 20151
Last Week
Last Week we successfully drew the smiley face!
This week we will expand on that:
Draw different types of faces
Draw stick figures
Topics
Function Parameters
Conditional Statements
Recursive Tree
Face 2
Mouth shape is smile
Eye radius is 25
Slightly different than last week
We could write a new functionfor eye radius of 25
What is we have 10, 100, 1000…different eye radii?
Face 3
Mouth shape is frown
Eye radius is 5
Both mouth shape andeye radius has changed!
We can write a new function forthe mouth shape
This can lead to duplicate code
Face 4
Mouth shape is frown
Eye radius is 25
Same as Face 3 butdifferent eye radius
We can make a whole newfunction for this too….
Four faces
Does four faces mean four functions?
What if there was user input to determine:
Mouth shape
Eye radius
How would we determine which of the four face functions to call?
Function parameters and conditionals will make it easy!
Face Similarities/Differences
What is the same between the faces?
Face shape and size
Location of the mouth center
Location, shape, and size of nose
Shape of eyes and location of their bottoms
What is different between the faces?
Shape of the mouth
Size (radius) of each eye
Functions w/ Parameter(s)
What is a parameter?
They are listed names in a function definition in the ()
They are used to provide information to the function
These can make the function behave differently
Functions w/ Parameter(s)
Recall from last week:
def drawEye(): turtle.down() turtle.circle( 15 ) turtle.up()
This draws an eye with a fixed radius of 15
What if we do not always want 15?
Do we make a function for every radius we want?
Say I want 300 different radii?
Functions w/ Parameter(s)
We can do any size eye with a single function:
def drawEye( size ): turtle.down() turtle.circle( size ) turtle.up()
size is a function parameter
To call drawEye we now must provide a value for size
drawEye( 10 ) will draw an eye with radius 10
In the function definition any where size is used will be replaced with the value passed into the function call
This allows almost and infinite number of sizes with a single function!
Functions w/ Parameter(s)
We can do any size eye with a single function:
def drawEye( 10 ): turtle.down() turtle.circle( 10 ) turtle.up()
size is a function parameter
To call drawEye we now must provide a value for size
drawEye( 10 ) will draw an eye with radius 10
In the function definition any where size is used will be replaced with the value passed into the function call
This allows almost and infinite number of sizes with a single function!
Functions w/ Parameter(s)
Changing the eye radius is simple
How about changing the shape of the mouth?
We simply cannot just change a value
Functions w/ Parameters
Recall from last week:
def drawMouth(): turtle.forward( 40 ) turtle.left( 65 ) turtle.forward( 30 ) turtle.left( 180 ) turtle.down() turtle.forward( 30 ) turtle.left( 50 ) turtle.forward( 30 ) turtle.left( 180 ) turtle.up() turtle.forward( 30 ) turtle.left( 65 ) turtle.forward( 40 ) turtle.left( 180 )
Functions w/ Parameters
Can we simply add a parameter and use it?
def drawMouth( shape ): turtle.forward( 40 ) turtle.left( 65 ) turtle.forward( 30 ) turtle.left( 180 ) turtle.down() turtle.forward( 30 ) turtle.left( 50 ) turtle.forward( 30 ) turtle.left( 180 ) turtle.up() turtle.forward( 30 ) turtle.left( 65 ) turtle.forward( 40 ) turtle.left( 180 )
Where do we use the shape parameter?
Conditionals
Conditionals
They choose among code blocks
Code blocks are choose based on conditions
Think of a traffic light
If it is green you go
If it is red you stop
If it is yellow you go very very fast
This is a conditional
Conditionals in Python
if statement
The most basic conditional
Example:
def reactToLight( lightColor ):
Conditionals in Python
if statement
The most basic conditional
Example:
def reactToLight( lightColor ): if lightColor is “red”: print(“stop”)
Conditionals in Python
if statement
The most basic conditional
Example:
def reactToLight( lightColor ): if lightColor is “red”: print(“stop”) elif lightColor is “green”: print(“go”)
Conditionals in Python
if statement
The most basic conditional
Example:
def reactToLight( lightColor ): if lightColor is “red”: print(“stop”) elif lightColor is “green”: print(“go”) elif lightColor is “yellow”: print(“floor it!”)
Conditionals in Python
if statement
The most basic conditional
Example:
def reactToLight( lightColor ): if lightColor is “red”: print(“stop”) elif lightColor is “green”: print(“go”) elif lightColor is “yellow”: print(“floor it!”) else: print(“WTF!”)
Conditionals in Python
How would this help us with the mouth??
Think about it:
If mouth shape is smile, draw ….
If mouth shape is frown, draw ….
Else, draw some other mouth shape
Conditionals in Python
drawMouth with conditional and parameter
def drawMouth( shape ): if shape is “smile”: #draw smile elif shape is “frown”: #draw frown else: #draw some other shape
Making a face
What do we need to make a face:
Main function
Prompts the user for mouth shape
Prompts the user for eye radius
Initializes the canvas (turtle)
Draws the face using the user input
Tells the user to hit Enter after done viewing the face
Close the drawing canvas
Making a face
What do we need to make a face:
drawFace function
Parameters, eye radius and mouth shape
Draw the border
Draw the mouth with given shape
Draw the nose
Draw the eyes with the given radius
Making a face
What do we need to make a face:
drawBorder function
Draws the border of the face
drawMouth function
Parameters, mouth shape
If mouth shape is smile, draw smile
If mouth shape is frown, draw frown
Otherwise draw “grimace”
Making a face
What do we need to make a face: drawNose function
Draw the nose
drawEyes function
Parameters, eye radius
Move to bottom of one eye
Call drawEye function with eye radius
Move to bottom of the other eye
Call drawEye function with eye radius
Move to bottom of face
drawEye function
Parameter, eye radius
Draw eye with given radius
Recursive Tree
Recursion Is the act of calling a function within itself
Example:
def countdown( start ): print(start) countdown (start – 1)
Anyone see an potential issue with this? How would I fix it?
It will never stop!
Fix:
def countdown( start ): print(start) if start > 0: countdown (start – 1)