9.4.2. Practice: Functions#
Create a function called say_goodbye which displays the text “Goodbye!”
# TODO: enter your code here
Write code that calls the function say_goodbye
# TODO: enter your code here
Redefine the function say_goodbye to take a parameter called name, and have it display “Goodbye name!” where “name” is replaced by whatever was in the name variable
# TODO: enter your code here
Write code that calls the function say_goodbye but with your name as a parameter
# TODO: enter your code here
Try out the code below which counts from 0 to 4 slowly:
import time # We need the time library for the following examples
for i in range(5):
print(i)
time.sleep(1)
0
1
2
3
4
We can put that for loop in a function like this:
def counter():
for i in range(5):
print(i)
time.sleep(1)
And then we can call it:
counter()
0
1
2
3
4
Now redifine counter by
copying the code above which defines
countermake the counter take a parameter called
maxHave the
rangecall use the parametermax
# TODO: enter your code here
Now try calling the new version of counter but passing it the argument 7
# TODO: enter your code here
Create a function called multiply which takes two arguments, multiplies them together (*), and then returns the multiplied value
# TODO: enter your code here
Call the mutliply function with two numbers and save the result in a variable. Then print out the variable to see that the multiplied number was saved.
# TODO: enter your code here