Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 2 Fancy Face, Conditional Execution, Recursive Tree Computer Science I Scott C Johnson Fall 20151.

Similar presentations


Presentation on theme: "Week 2 Fancy Face, Conditional Execution, Recursive Tree Computer Science I Scott C Johnson Fall 20151."— Presentation transcript:

1 Week 2 Fancy Face, Conditional Execution, Recursive Tree Computer Science I Scott C Johnson Fall 20151

2 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

3 Goal of this lecture

4 Face 1  Mouth shape is smile  Eye radius is 15  We did this last week

5 Face 2  Mouth shape is smile  Eye radius is 25  Slightly different than last week  We could write a new function for eye radius of 25  What is we have 10, 100, 1000… different eye radii?

6 Face 3  Mouth shape is frown  Eye radius is 5  Both mouth shape and eye radius has changed!  We can write a new function for the mouth shape  This can lead to duplicate code

7 Face 4  Mouth shape is frown  Eye radius is 25  Same as Face 3 but different eye radius  We can make a whole new function for this too….

8 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!

9 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

10 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

11 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?

12 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!

13 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!

14 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

15 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 )

16 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?

17 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

18 Conditionals in Python  if statement  The most basic conditional  Example: def reactToLight( lightColor ):

19 Conditionals in Python  if statement  The most basic conditional  Example: def reactToLight( lightColor ): if lightColor is “red”: print(“stop”)

20 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”)

21 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!”)

22 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!”)

23 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

24 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

25 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

26 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

27 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”

28 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

29 Execution Diagram

30 Demo/Live code  What does the code now look like?

31 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)

32 Recursive Tree  When to use recursion?  Example: Recursive Tree  White Board Demo of Branching Tree


Download ppt "Week 2 Fancy Face, Conditional Execution, Recursive Tree Computer Science I Scott C Johnson Fall 20151."

Similar presentations


Ads by Google