Practice and reinforce the concepts from Lesson 8
Estimated time: 30-40 minutes
:computer: Coding Setup:
:bulb: Make sure you're signed in to your Google account before clicking the link!
Question One: Basic Function Creation
Time estimate: 5 minutes
:computer: Your Task:
- Create a function named
welcome1
- Make the function print the message "Hi, welcome to Telebort!" when called
- Call the function to test it
Steps to follow:
- Define the function using
def welcome1():
- Inside the function, use
print()
to display the message- Call the function by typing its name with parentheses
pythonwelcome1()
Expected output:
outputHi, welcome to Telebort!
Question 2: Functions with Default Parameters
Time estimate: 8 minutes
:computer: Your Task:
- Create a function named
welcome2
with a parameter that has a default value- When called without arguments: print "Hi, welcome to Telebort!"
- When called with a name argument: print "<name>, welcome to Telebort!"
- Test the function both ways
Steps to follow:
- Define the function with a parameter (e.g.,
def welcome2(name=None):
)- Use an
if
statement to check if a name was provided- Print the appropriate message based on the condition
- Call the function twice - once without arguments, once with your name tip Hint: You can set a default parameter value like this:
def function_name(parameter=default_value):
welcome2()
welcome2("Chong Wei")
Expected output:
Hi, welcome to Telebort!
Chong Wei, welcome to Telebort!
Time estimate: 7 minutes
:computer: Your Task:
multiple
that takes two parameters: num1
and num2
Steps to follow:
def multiple(num1, num2):
return
to send the result back:bulb: Remember:
return
sends a value back from the function, whileprint()
just displays it. You'll need to print the returned value when you call the function!
python
print(multiple(2,3))
Expected output:
output6
Question 4: Functions with Keyword Arguments
Time estimate: 10 minutes
:computer: Your Task:
- Create a function named
register
with three parameters:name
,age
, andprog
- The function should print three lines of information about the registration
- Call the function using keyword arguments
Steps to follow:
- Define the function:
def register(name, age, prog):
- Inside the function, print three lines:
- Line 1: "Name: " followed by the name
- Line 2: "Age: " followed by the age
- Line 3: "You have successfully registered in Program " followed by the program letter
- Call the function using keyword arguments (e.g.,
name="Your Name"
) tip Keyword arguments let you specify which value goes to which parameter by name, making your code clearer and allowing you to provide arguments in any order!
register(name="Chong Wei", age=15, prog="F")
Expected output:
Name: Chong Wei
Age: 15
You have successfully registered in Program F
Function not defined error?
Unexpected output?
print()
inside functions that need to display textreturn
instead of print()
inside the functionSyntax errors?
:
:information_source: Info :bulb: Pro tip: Test each function immediately after writing it to catch errors early!
:warning: Warning :warning: Before submitting:
- Test all four functions to ensure they work correctly
- Save your Colab notebook (File -> Save)
- Double-check that your output matches the expected results
- Make sure your notebook includes your name
:emoji:️ Ready to submit?
:link: Submit your exercise here
:information_source: Info :memo: Your submission helps us track your progress and provide personalized support if needed.