Course Content
Python Functions Tutorial
Python Functions Tutorial
1. What is a Function in Python?
2. Positional and Optional Arguments
4. Function Return Value Specification
Challenge: Positional Arguments
Task
Swipe to start coding
Implement the register_user
function that checks the user's age, adds their details to a database (users_db
), and returns a success or failure message.
- Define the function
register_user
that takes parametersusername
,email
, andage
. - Inside the function
register_user
, check if age is less than 18. If it is, return the message"Registration failed: age must be 18 or older."
- Create a dictionary user with the keys
username
,email
, andage
, and assign the corresponding values. - Add the user dictionary to the
users_db
list using the appropriate method. - If everything is successful, return the message
"User {username} registered successfully!"
, where{username}
is the actual username. - Call the
register_user
function with example parameters for a user, either by passing arguments directly or as a dictionary.
Solution
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 2
Challenge: Positional Arguments
Task
Swipe to start coding
Implement the register_user
function that checks the user's age, adds their details to a database (users_db
), and returns a success or failure message.
- Define the function
register_user
that takes parametersusername
,email
, andage
. - Inside the function
register_user
, check if age is less than 18. If it is, return the message"Registration failed: age must be 18 or older."
- Create a dictionary user with the keys
username
,email
, andage
, and assign the corresponding values. - Add the user dictionary to the
users_db
list using the appropriate method. - If everything is successful, return the message
"User {username} registered successfully!"
, where{username}
is the actual username. - Call the
register_user
function with example parameters for a user, either by passing arguments directly or as a dictionary.
Solution
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 2