In this mini-lab you will gain practice with variables, and defining and using functions in Python.
Editor
in Spyder (or whichever development
environment you are using), create a new file for the functions you will
write in
this mini-lab. Then save the new file with a name
representative of this minilab.
Be sure to add
comments with your name and the date, and as well
as a comment that describes this file.
average
),
and add a comment above it that describes its purpose.
input
function always returns
the user's input as a string unless you convert it to another
type.
Editor
.
Stop and Think: What happened when you clicked the green arrow? (If nothing happened, do not fear; nothing should happen yet! Continue to the next exercise.)
iPython console
call your function by typing its name, along with empty
parentheses.Stop and Think: Did your program work correctly now? If not, what kinds of things did you have to fix, and why?
Stop and Think: What are the advantages and disadvantages of running your program in script mode vs interactive mode?
Debugging
Tip:
When you call a function, 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.
The function you just wrote may be frequently handy, but we can add something to it to make it even more useful. As it stands now, you can use it to choose two numbers and display their average, but you would not be able to do anything further with that average once the function has completed its job. Outside the function you would have not have access to the values of either of the numbers, nor the average value. To make it more useful, we will have it return the average.
Editor
, edit your function by first
commenting out the statement that print the average value, and then adding the following return statement as the
last line of the
function:
return avgwhere
avg
is the variable that holds the
average that you computed. (Or in place of the avg
variable, you may
have the actual computation of the average.)
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
average it has computed.
Stop and Think: Did you get the same output? Why or why not? Will you be able to use the average value after your function has finished its job?
In[5]: average = computeAvg()
In[6]: print("The average was: ", average)(This is referred to as "capturing" the return value in a variable.)
Let's generalize your function so that it will compute the average of any two numbers that get passed to it as a parameters and will return the average of those two numbers.
In[7]: average(12, 15)
In[8]: average(num1, num2)where num1 and num2 are variables holding some particular numbers.
Write a new function that takes three words, a noun, a verb, and an adjective, as parameters and displays a short story constructed using the words passed in to the function. A sample session might look like the following:
In[9]: madlib("monkey", "jump", "happy") When I was walking to work today I saw a monkey! It was very happy and it made me want to jump. OR It's your turn to jump over the happy monkey.