python programming using the jes picture functions and defining new functions

32
Python programming Using the JES picture functions and defining new functions

Upload: jocelyn-waters

Post on 20-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

Functions in general F(a,b) 4‘a’ F(4, ‘a’) side-effects

TRANSCRIPT

Page 1: Python programming Using the JES picture functions and defining new functions

Python programming

Using the JES picture functions and defining new functions

Page 2: Python programming Using the JES picture functions and defining new functions

Review• Python – what kind of animal?• JES environment• command area• arithmetic• print command• variables and the assignment statement• Functions

– sqrt()– str()– showInformation()– requestInteger()

Page 3: Python programming Using the JES picture functions and defining new functions

Functions in general

F(a,b)

4 ‘a’

F(4, ‘a’)

side-effects

Page 4: Python programming Using the JES picture functions and defining new functions

Example: sqrt()

sqrt(a)

4

2

side-effects

Page 5: Python programming Using the JES picture functions and defining new functions

Example: showInformation()

showInformation(message)

message

Pops up a new window displaying the message text

Page 6: Python programming Using the JES picture functions and defining new functions

Example: requestNumber()

requestNumber(“What is the answer to Life, the Universe

and Everything?” )

“What is the answerto Life, the Universe

and Everything?”

Pops up a new windowdisplaying the message text

with an input box where the usercan type in a number, e.g., 42

42

Page 7: Python programming Using the JES picture functions and defining new functions

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

and sound manipulation– makeEmptyPicture()– makePicture()– makeSound()– show()– play() – pickAFile()

• Some of these functions accept input valuespic1 = makeEmptyPicture(300,200)show(pic1)

Page 8: Python programming Using the JES picture functions and defining new functions

Example: makeEmptyPicture()

makeEmptyPicture(300,200)

300 x 200 blank image

300

makePicture(width,height)• creates and returns a picture object of the given dimensions

200

Page 9: Python programming Using the JES picture functions and defining new functions

Example: show()

show(picture-object)

picture-object(an encoding of an image)

Pops up a new window displaying image stored

in picture-object

Page 10: Python programming Using the JES picture functions and defining new functions

Picture Functions

• makePicture(filename) creates and returns a picture object, from the JPEG file at the filename

• show(picture) displays a picture in a window

• We’ll learn functions for manipulating pictures later, like getColor(), setColor(), and repaint()

Page 11: Python programming Using the JES picture functions and defining new functions

Example: pickAFile()

pickAFile()

Filename (eg: C:\Documents and Settings\mpapalas\My Documents\greenroom.jpg)

Pops up a dialogbox for the user to

select a file

Page 12: Python programming Using the JES picture functions and defining new functions

Example: makePicture()

makePicture(filename)

Picture object corresponding to image that is saved in theFile

theFile (a string containing a file name)

makePicture(filename)• creates and returns a picture object, from the JPEG file at the filename

Page 13: Python programming Using the JES picture functions and defining new functions

Demonstrating picture manipulation with JES

>>> >>> print pickAFile()C:\Documents and Settings\mpapalas\Desktop\sample.jpg>>> theFile = "C:\Documents and Settings\mpapalas\Desktop\sample.jpg">>> makePicture(theFile)Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600>>> print makePicture(theFile)Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600>>> pic = makePicture(theFile)>>> print picPicture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600>>> show(pic)

Page 14: Python programming Using the JES picture functions and defining new functions

Writing a recipe: Making our own functions

def fcnName (input1, input2,...) :

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 (“(input1)”, 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

Page 15: Python programming Using the JES picture functions and defining new functions

Writing a recipe: Making our own functions

def fcnName (input1, input2,...) :

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 (“(input1)”)• The body of the recipe is indented (Hint: Use two

spaces)– That’s called a block– Optionally, the function can return a value

•Important: End the line with a colon (“:”)

Page 16: Python programming Using the JES picture functions and defining new functions

Writing a recipe: Making our own functions

def fcnName (input1, input2,...) :

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 (“(input1)”)• The body of the recipe is indented (Hint: Use two

spaces)– That’s called a block– Optionally, the function can return a value

Optional

Page 17: Python programming Using the JES picture functions and defining new functions

Using a recipe: Invoking our own functions

• Once a function has been defined…

def fcnName (input1, input2,...) :

block describing what the function should do return value

… it can be used with any input values of the appropriate type:

result1 = fcnName(300, 200)

Page 18: Python programming Using the JES picture functions and defining new functions

Example: A recipe for displaying picked picture files:

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)

Page 19: Python programming Using the JES picture functions and defining new functions

pickAFile() Pops up a dialogbox for the user to

select a file

theFile

A recipe for displaying

picked picture files:

def pickAndShow(): theFile = pickAFile()

Page 20: Python programming Using the JES picture functions and defining new functions

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile)

A recipe for displaying

picked picture files:

Page 21: Python programming Using the JES picture functions and defining new functions

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

show(picture-obj)Pops up a new window displaying image stored in the the picture object

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)

A recipe for displaying

picked picture files:

Page 22: Python programming Using the JES picture functions and defining new functions

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

show(picture-obj)Pops up a new window displaying image stored in the the picture object

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)

A recipe for displaying

picked picture files:

Page 23: Python programming Using the JES picture functions and defining new functions

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

show(picture-obj)Pops up a new window displaying image stored in the the picture object

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic) pic

kAnd

Show()

A recipe for displaying

picked picture files:

Page 24: Python programming Using the JES picture functions and defining new functions

A recipe for playing picked sound files

def pickAndPlay(): myfile = pickAFile() mysound = makeSound(myfile) play(mysound)

Page 25: Python programming Using the JES picture functions and defining new functions

Anything complicated is best done in the Program Area

Program Area

Command Area

Program area- A simple editor for programs

Command area- Interaction with Jython

Page 26: Python programming Using the JES picture functions and defining new functions

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

and 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

Page 27: Python programming Using the JES picture functions and defining new functions

Making functions the easy way

• Get something working by typing commands

• Enter the def command.• Copy-paste the right commands into the

program area

Page 28: Python programming Using the JES picture functions and defining new functions

Names for variables and functions can be (nearly)

anything!• Must start with a letter (but can contain numerals)• Can’t contain spaces

– myPicture 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

– MyPicture is not the same as myPicture or mypicture• Sensible names are sensible

– E.g. myPicture 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

Page 29: Python programming Using the JES picture functions and defining new functions

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.

Page 30: Python programming Using the JES picture functions and defining new functions

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 pickAndShow() in the command area

Page 31: Python programming Using the JES picture functions and defining new functions

Reading

• Sections 2.4, 2.5 from “Introduction to Computing and Programming in Python”

Page 32: Python programming Using the JES picture functions and defining new functions

AssignmentCreate a function that…

– Causes an interactive input window to pop up and request some form of input from the user and stores it in a variable

– Displays the value stored in the variable– Displays an image

• Save your function in a file• Load it• Use the function in the command area to

make sure that it works and does what you want it to do

• Submit your file through webct