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

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

Jan 02, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 2 Fancy Face, Conditional Execution, Recursive Tree Computer Science I Scott C Johnson Fall 20151.

Week 2Fancy Face, Conditional

Execution, Recursive Tree

Computer Science I

Scott C Johnson

Fall 20151

Page 2: Week 2 Fancy 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

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

Goal of this lecture

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

Face 1

Mouth shape is smile

Eye radius is 15

We did this last week

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

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?

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

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

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

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….

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

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!

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

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

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

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

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

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?

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

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!

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

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!

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

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

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

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 )

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

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?

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

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

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

Conditionals in Python

if statement

The most basic conditional

Example:

def reactToLight( lightColor ):

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

Conditionals in Python

if statement

The most basic conditional

Example:

def reactToLight( lightColor ): if lightColor is “red”: print(“stop”)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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”

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

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

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

Execution Diagram

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

Demo/Live code

What does the code now look like?

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

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)

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

Recursive Tree

When to use recursion?

Example: Recursive Tree

White Board Demo of Branching Tree