In this activity you will learn how to use Google Colab to write Python code. You will experiment with writing a few functions of your own.
computeAvg
that will ask the user for
two numbers and then print out the average of those two numbers.
In order to ask the user for two numbers, you should have two
input
statements, with the values that the user enters
being stored in variables. You should store the result of the
average calculation in another variable. For example, you may have something
like the following as the first statement in your function:
num1 = float(input("Please enter a number:
"))
to get a value from the user.
computeAvg()
.
computeAvg
function that you just wrote, the function printed the average. To
make this function more useful, we will have it return the average
instead of printing it. Comment out your print statement by putting
a #
symbol in front of it. Then add the following statement
at the end of your function:
return avg
where
avg
is the name of the variable that holds the result
of the average computation.
average = computeAvg()
print("The average was: ",average)
num1
and num2
, separated by
commas between the parentheses in the first line of the function
definition. Modify your function by commenting out the user input statements. Print out the
two numbers and then return the average.
average = computeAvg(12, 15)
- Add two more lines in that Code cell to get user input for the two numbers and store those values in variables. Then call the function as you have done before, but pass in the user's values. This should look something like:
val1 = float(input("Enter a value: "))
val2 = float(input("Enter another value: "))
average = computeAvg(val1, val2)
print("Average is: ",average)- Mad Libs: A Mad Lib is a fun word game, in which you construct a nonsensical short story - or in our case, just a sentence or two - by filling in random words in certain spots. See the Wikipedia article for more information.
In a new Code cell, 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. Then in another Code cell, test your function by calling it with three random words. A sample session might look something like the following:
Function call:
madlib("monkey", "jump", "happy")
Results:
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.
- Test your function several more times by calling it with different words.
- Add a Text Cell at the top of your file with your name, the date, and a short description of this in-class activity.
- Enhancements/Challenges: (For fun, not required) Think about how you might write another function that solicits the three words from the user. Then write a third function that uses these two functions, getting the input from one function and passing it into the other function. What other ways could you improve on your mad lib?
.ipynb
file) on
Kit. Your file should show some work from the tutorials, followed
by a function to compute the average of two numbers and a function to
create a MadLib. Your name, date, and assignment description should
be in a Text cell at the top.