1 / 26

Python programming

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().

artiem
Download Presentation

Python programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python programming Using the JES picture functions and defining new functions

  2. Review • Python – what kind of animal? • JES environment • command area • arithmetic • print command • variables and the assignment statement • Functions • sqrt() • str() • showInformation() • requestInteger()

  3. More JES Functions • Some pre-defined functions in JES for picture and sound manipulation • makeEmptyPicture() • makePicture() • makeSound() • show() • play() • pickAFile() • Some of these functions need parameter values:

  4. Examples: Manipulating Pictures makeEmptyPicture(width,height) creates a new empty picture show(picture) displays a picture in a separate window >>> pic1 = makeEmptyPicture(200,100) >>> show(pic1) >>> setAllPixelsToAColor(pic1, red) >>> show(pic1) >>> addText(pic1,30,50,“hello") • similarly can add rectangles, lines, etc.

  5. More Picture Functions • makePicture(filename) creates and returns a picture object, from the JPEG file at the filename • We’ll learn functions for manipulating pictures later, like getColor(), setColor(), and repaint()

  6. 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 pic Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600 >>> show(pic)

  7. Common errors >>> file = pickAFile() >>> pic = makePicture(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 Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600 Oops!

  8. Writing a recipe: Making our own functions def fcnName (input1, input2,...) : block describing what the function should do returnvalue • 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

  9. Writing a recipe: Making our own functions • Important: End the line with a colon (“:”) def fcnName (input1, input2,...) : block describing what the function should do returnvalue • 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

  10. Writing a recipe: Making our own functions def fcnName (input1, input2,...) : block describing what the function should do returnvalue • 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

  11. Using a recipe: Invoking our own functions • Once a function has been defined… def fcnName (input1, input2,...) : block describing what the function should do returnvalue … it can be used with any input values of the appropriate type: result1 = fcnName(300, 200)

  12. Example: A recipe for displaying picked picture files: defpickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)

  13. A recipe for playing picked sound files def pickAndPlay(): myfile = pickAFile() mysound = makeSound(myfile) play(mysound)

  14. Anything complicated is best done in the Program Area Program area - A simple editor for programs • Command area • Interaction with Jython

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

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

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

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

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

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

  21. Assignment Create a function that… • Pops up a welcome message to the user of your program. (use showInformation()) • 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 requestString()) • 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 showInformation()) • Gets the image file from the user (use pickAFile()) • Shows the image with whatever the user typed in superimposed over it. (use MakePicture(), addText(), and show()) • Pops up a goodbye message. (use showInformation()) • 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

  22. Example – go() start…Step 1: Pops up a welcome message to the user of your program. (use showInformation())

  23. Example – go() requesting input…Step 2: Uses an interactive input window to request some form of input from the user and stores it in a variable. (use requestString())

  24. Example – go() using the input…Step 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 showInformation())

  25. Example – go() selecting an image file…Step 4: Gets the image file from the user (use pickAFile())

  26. Example – go() showing image and goodbye message.Step 5: Shows the image with whatever the user typed in superimposed over it. (use MakePicture(), addText(), and show())Step 6: Pops up a goodbye message. (use showInformation())

More Related