Python programming Using the JES picture functions and

  • Slides: 26
Download presentation
Python programming Using the JES picture functions and defining new functions

Python programming Using the JES picture functions and defining new functions

Review • • Python – what kind of animal? JES environment command area arithmetic

Review • • Python – what kind of animal? JES environment command area arithmetic print command variables and the assignment statement Functions – – sqrt() str() show. Information() request. Integer()

More JES Functions • Some pre-defined functions in JES for picture and sound manipulation

More JES Functions • Some pre-defined functions in JES for picture and sound manipulation – make. Empty. Picture() – make. Sound() – show() – play() – pick. AFile() • Some of these functions need parameter values:

Examples: Manipulating Pictures make. Empty. Picture(width, height) creates a new empty picture show(picture) displays

Examples: Manipulating Pictures make. Empty. Picture(width, height) creates a new empty picture show(picture) displays a picture in a separate window >>> pic 1 = make. Empty. Picture(200, 100) >>> show(pic 1) >>> set. All. Pixels. To. AColor(pic 1, red) >>> show(pic 1) >>> add. Text(pic 1, 30, 50, “hello")

More Picture Functions • make. Picture(filename) creates and returns a picture object, from the

More Picture Functions • make. Picture(filename) creates and returns a picture object, from the JPEG file at the filename • We’ll learn functions for manipulating pictures later, like get. Color(), set. Color(), and repaint()

Demonstrating picture manipulation with JES >>> print pick. AFile() C: Documents and SettingsmpapalasDesktopsample. jpg

Demonstrating picture manipulation with JES >>> print pick. AFile() C: Documents and SettingsmpapalasDesktopsample. jpg >>> the. File = "C: Documents and SettingsmpapalasDesktopsample. jpg" >>> make. Picture(the. File) Picture, filename C: Documents and SettingsmpapalasDesktopsample. jpg height 1200 width 1600 >>> print make. Picture(the. File) Picture, filename C: Documents and SettingsmpapalasDesktopsample. jpg height 1200 width 1600 >>> pic = make. Picture(the. File) >>> print pic Picture, filename C: Documents and SettingsmpapalasDesktopsample. jpg height 1200 width 1600 >>> show(pic)

Common errors >>> file = pick. AFile() >>> pic = make. Picture(file) >>> show(file)

Common errors >>> file = pick. AFile() >>> pic = make. Picture(file) >>> show(file) The error was: sample Name not found globally. A local or global name could not be found. You need to define the function or variable before you try to use it in any way. >>> show(pic) >>> print pic Picture, filename C: Documents and SettingsmpapalasDesktopsample. jpg height 1200 width 1600 Oops!

Writing a recipe: Making our own functions def fcn. Name (input 1, input 2,

Writing a recipe: Making our own functions def fcn. Name (input 1, input 2, . . . ) : block describing what the function should do return value • To make a function, use the command def • Then, the name of the function, and the names of the parameters (input values) between parentheses (“(input 1)”, etc), separated by commas. • The body of the recipe is indented (Hint: Use two spaces) – That’s called a block – Optionally, the function can return a value

Writing a recipe: Making our own functions def fcn. Name (input 1, input 2,

Writing a recipe: Making our own functions def fcn. Name (input 1, input 2, . . . ) : • Important: End the line with a colon (“: ”) block describing what the function should do return value • To make a function, use the command def • Then, the name of the function, and the names of the input values between parentheses (“(input 1)”) • The body of the recipe is indented (Hint: Use two spaces) – That’s called a block – Optionally, the function can return a value

Writing a recipe: Making our own functions def fcn. Name (input 1, input 2,

Writing a recipe: Making our own functions def fcn. Name (input 1, input 2, . . . ) : block describing what the function should do return value Optional • To make a function, use the command def • Then, the name of the function, and the names of the input values between parentheses (“(input 1)”) • The body of the recipe is indented (Hint: Use two spaces) – That’s called a block – Optionally, the function can return a value

Using a recipe: Invoking our own functions • Once a function has been defined…

Using a recipe: Invoking our own functions • Once a function has been defined… def fcn. Name (input 1, input 2, . . . ) : block describing what the function should do return value … it can be used with any input values of the appropriate type: result 1 = fcn. Name(300, 200)

Example: A recipe for displaying picked picture files: def pick. And. Show(): the. File

Example: A recipe for displaying picked picture files: def pick. And. Show(): the. File = pick. AFile() pic = make. Picture(the. File) show(pic)

A recipe for playing picked sound files def pick. And. Play(): myfile = pick.

A recipe for playing picked sound files def pick. And. Play(): myfile = pick. AFile() mysound = make. Sound(myfile) play(mysound)

Anything complicated is best done in the Program Area Program area - A simple

Anything complicated is best done in the Program Area Program area - A simple editor for programs Command area

Using the Program Area • Type your program (or cut and paste from the

Using the Program Area • Type your program (or cut and paste from the command area) • Save (or Save As) - use. py for file extension • Load Program (click on button between command program areas) – – Before you load it, the program is just a bunch of characters. Loading encodes it as an executable function You must Save before Loading You must Load before you can use your function

Making functions the easy way • Get something working by typing commands • Enter

Making functions the easy way • Get something working by typing commands • Enter the def command. • Copy-paste the right commands into the program area

Names for variables and functions can be (nearly) anything! • Must start with a

Names for variables and functions can be (nearly) anything! • Must start with a letter (but can contain numerals) • Can’t contain spaces – my. Picture is okay but my Picture is not • Be careful not to use command names as your own names – print = 1 won’t work – (Avoid names that appear in the editor pane of JES highlighted in blue or purple) • Case matters – My. Picture is not the same as my. Picture or mypicture • Sensible names are sensible – E. g. my. Picture is a good name for a picture, but not for a picture file. – x could be a good name for an x-coordinate in a picture, but probably not for anything else

Blocking is indicated for you in JES • Statements that are indented the same,

Blocking is indicated for you in JES • Statements that are indented the same, are in the same block. • Statements that are in the same block as where the line where the cursor is are enclosed in a blue box.

Now what? • After a program with a function definition is loaded, that function

Now what? • After a program with a function definition is loaded, that function can be used either from the command or the program area • Try using your function by typing pick. And. Show() in the command area

Reading • Sections 2. 4, 2. 5 from “Introduction to Computing and Programming in

Reading • Sections 2. 4, 2. 5 from “Introduction to Computing and Programming in Python”

Assignment Create a function that… 1. Pops up a welcome message to the user

Assignment Create a function that… 1. Pops up a welcome message to the user of your program. (use show. Information()) 2. Uses an interactive input window to request some form of input from the user and stores it in a variable. For example, ask them to type in their name. (use request. String()) 3. Pops up another message window where the message contains a word typed in by the user, and also informs them that they will have to select an image file. (use show. Information()) 4. Gets the image file from the user (use pick. AFile()) 5. Shows the image with whatever the user typed in superimposed over it. (use Make. Picture(), add. Text(), and show()) 6. Pops up a goodbye message. (use show. Information()) • • Be sure to test your function to verify that it works as intended Show your function to one of your classmates before demonstrating to instructor/TA

Example – go() showing image and goodbye message. Step 5: Shows the image with

Example – go() showing image and goodbye message. Step 5: Shows the image with whatever the user typed in superimposed over it. (use Make. Picture(), add. Text(), and show()) Step 6: Pops up a goodbye message. (use show. Information())