In this mini-lab you will practice declaring variables, defining, and using functions in Python. You will explore source code management by developing a main function which will be the driver of your code. At the end of this mini-lab you'll create a source file to keep your functions in. You'll then import these functions the same way you imported jes4py.
from jes4py import *
at the top of your
file). Next, add the following code to your main.py file.
if __name__ == '__main__':
print("Hello World!")
import
statement, and then type this function into
your file. NOTE:
Indentation and capitalization are very important in Python -- in
this case, indentation indicates that the statements are part of the
function -- so be sure to indent and capitalize (or not) just as in
the reading. print("Hello World!")
in the driver portion of
your program with this function. It should look like this:
if __name__ == '__main__':
pickAndShow()
from time import *
at
the top of your file, and then the statement
time.sleep(5)
after your the show
statement; or 2) Go to the terminal and do a Command-Click on the
filename.)
What happened when you ran the function? 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 run your program again.
Analysis Questions: Does this new function have any variables? If so, how many? Does it call any other functions?
NOTE: You should include the answers to analysis questions from this mini-lab and any other mini-lab as comments (see next exercise) in your program file.
Finally, move the function pickAndShow below the main driver. What happens when you run the code? Once you've documented your response, undo this change.
For each function, add a comment immediately above the definition similar to the following:# Mini-Lab: Exploring Functions # Author: "Your Name" # Date: Today's Date # Group or partner (If applicable) # With Assistance From: "Give credit to the people who aided you."
# This function will pick a picture file and show it
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. Test this out by adding the following lines to your main and document the behavior.
show(myPict) newPict = makePicture(myFile) show(newPict)
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.)
pickAndShow
functon. Run your
program.picture
.
pickAndShow
function, write a new function that will pick a sound,
play it, and then return it. Give the function a name that indicates
its purpose (such as pickAndPlay
),
and add a comment above it that describes its purpose.
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
do this.)
showAndPrint
. (You
can copy and paste your first pickAndShow
function as a starting point.) Add
a parameter name between the parentheses in 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 print
command from
Wednesday's minilab?)
showAndPrint(myPict)
Debugging Tip: Did calling the function give you a NameError error rather than showing the picture? If you have not definedmyPict
as a picture object in main yet, the function will not recognize the value ofmyPict
. (Refer back to Wednesday's minilab if you have forgotten how to do this.)
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.
showAndPlay(myPict, mySound)
will show the picture represented by the myPict
variable
and will play the sound represented by the mySound
variable.