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
# Recipe 1 from p. 29: 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 mypict
Make 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.)
>>>show(picture)
picture.
printGreeting),
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
def line), 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.
You have written a nice, friendly function, but it is only friendly to you. Now let's generalize it to be friendly to anyone.
greetAnyone or printGreetingTo. (You
can copy and paste your first greeting function 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 person's name. Have the new function print "Hello, parameterName" (where parameterName should
be the actual name of the parameter to your function). Add a comment above
the function that describes its purpose.printGreetingTo("Chris")
Debugging Tip: Did the function
print "Hello, parameterName" rather than "Hello, Chris" or "Hello,
AnotherName"? If
the parameter name is within the quotation marks in your print statement,
then your function will print the parameter name rather than the value passed
in as a parameter. Remember that you can use the plus sign (+) to
join two strings. (The parameter value is a string.)
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.
You can generalize your function a little more to give it an international flavor.
print statement should
join the two parameters together, with a comma and space in between (the
comma and space must be in quotation marks).
greet("Bonjour", "Alyce")printsBonjour, Alyce
greet("Guten Tag", "Pam")printsGuten Tag, Pam
pickAndShow function.
In the previous exercises, you passed constant string values to functions as parameters. It is also possible to use variables or the values returned from function calls as parameters to functions. You can only use a function return value as the parameter to another function, though, if the parameter function has a return statement.
showNamed function on p. 33 of the textbook
and load it.
pickAFile. Then call
the showNamed function, passing it your new variable.
show(makePicture(pickAFile()))
Analysis Questions: What is the value being passed as a parameter tomakePicture? What is being passed as a parameter toshow?