In this mini-lab you will gain practice with variables, defining and using functions in Python, and the mechanism for saving and loading files in JES.
Analysis Questions: Does this function have any variables? If so, how many? Does it call any other functions?
Then leave a blank line and add a comment immediately above your function definition similar to the following:# Mini-Lab: Exploring Functions # Your Name # Today's Date
# This function will pick a picture file and show it
pickAndShow()in the command area and hit enter. What happened? Was it what you expected?
Debugging Tip: If the function does not work correctly, check that you remembered the colon at the end of the first line of the function definition and that you spelled all of your variable names and function names correctly. After making any corrections, remember to click on the Load button before calling your function again.
The pickAndShow function will frequently be handy in this class, but we can add something to it to make it even more useful. As it stands now, you can use it to choose a file and display it, but you would not be able to do anything further with the picture once the pickAndShow function has completed its job. Outside the function you would have access to neither myFile, the chosen filename, nor myPict, the picture object. To make it more useful, we will have it return the picture.
return myPictMake sure that the indentation is the same as for the other lines in the function. Edit the comment above the function to state that it returns the picture it has displayed.
>>>picture = pickAndShow()(This is referred to as "capturing" the return value in a variable.)
pickAndPlay), and add a comment above it that describes its purpose.
Debugging Tip: When you call a function in the command area, be sure to use the name of the function (the name you gave it in the
defline), not the name of the file in which you saved it. Those are two different things, and may have very different names.
Debugging Tip: A common error in JES is to forget to click on the Load button before calling a function.
Let's generalize the
pickAndShow function so that it will
show any picture that gets passed to it as a parameter and will print
the dimensions of that picture, and the full path of the file that was
used to make that picture. (See Wednesday's minilab if you forgot how to
showAndPrint. (You can copy and paste your first
pickAndShowfunction as a starting point.) Add a parameter name between the parentheses in the first line of the function definition. The name should indicate that the parameter represents a picture. Have the new function show the picture that is represented by the parameter and then print the full path and dimensions of that picture. (Remember the
Debugging Tip: Did calling the function give you an error and tell you Name not found globally? rather than showing the picture? If you have not defined
myPictas a picture object in the command area yet, the function will not recognize the value of
myPict. (Refer back to Wednesday's minilab if you have forgotten how to do this.)
Debugging Tip: Remember to click on the Load button anytime you make a change to your program definition.
Shortcut: You can use the up arrow in the command area to go back to the previous command and then hit enter to run that command.
Analysis Questions: How many times do you see the full path name and dimensions of the picture printed? Why does this happen?
Let's make a function that shows a picture and plays a sound.
will show the picture represented by the
myPictvariable and will play the sound represented by the