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

Slides:



Advertisements
Similar presentations
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Structured programming
The Turtle Laboratory Sequence LMICSE Workshop June , 2005 Alma College.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Main task -write me a program
CS 206 Introduction to Computer Science II 02 / 25 / 2009 Instructor: Michael Eckmann.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Algorithm & Flowchart.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Log on and Download from website:
1 Survey of Computer Science CSCI 110, Spring 2011 Lecture 14 Recursion.
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
Functions and subroutines – Computer and Programming.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Computer Science 112 Fundamentals of Programming II Graphics Programming.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
SOLIDWORKS: Lesson II – Revolutions, Fillets, & Chamfers UCF Engineering.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
CIT 590 Intro to Programming Lecture 3. Pair programming logistics You and your partner submit 1 assignment Figure out who the submitter will be PLEASE.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 4 Conditionals Turtles.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Welcome to Code Club You are going to be telling the computer what to do! Your pass to the club What level will you get to?
Getting started with the turtle Find the latest version of this document at
Class06 Conditional Statements MIS 3501, Fall 2015 Brad Greenwood, PhD MBA Department of MIS Fox School of Business Temple University 9/10/2015 © 2014,
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
ITEC 109 Lecture 18 Looping. Review Questions? Conditionals –if / elif / else –and / or / not.
Mark Dixon, School of Computing SOFT 120Page 1 1. User Defined Procedures.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Next Week… Quiz 2 next week: –All Python –Up to this Friday’s lecture: Expressions Console I/O Conditionals while Loops Assignment 2 (due Feb. 12) topics:
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Topic: Recursion – Part 2
Topic: Functions – Part 2
Writing Functions( ) (Part 5)
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Chapter 4 Functions Objectives
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
3. Decision Structures Rocky K. C. Chang 19 September 2018
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
no replacement – version 2
Week 6 Computer Programming Gray , Calibri 24
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Iteration – While Loops
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Week 6 Computer Programming Year 9 – Unit 9.04
Week 7 - Friday CS 113.
Defining Functions.
Presentation transcript:

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

Goal of this lecture

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

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?

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

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

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

Execution Diagram

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

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)

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