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: Optional Arguments
Task
Swipe to start coding
Implementing a function add_user
to manage users in a database. If the user exists in users_db
, their fields should be updated instead of creating a new entry. If the user is not found, a new user should be added with the provided details.
- Define the
add_user
function with parametersname
,age
,role
(default is "user"), andstatus
(default is "active"). - Check if a user with the same
name
exists in theusers_db
list. - If the user exists, update their
age
,role
, andstatus
with the values received as arguments in theadd_user
function. - Return the string
"User {name} updated successfully!"
after updating the user. - If the user doesn't exist, create a new user (
new_user
) with the provided details(name
,age
,role
,status
). - Add the new user (
new_user
) to listusers_db
. - Return the string
"User {name} added successfully!"
after adding the new user.
Solution
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 4
Challenge: Optional Arguments
Task
Swipe to start coding
Implementing a function add_user
to manage users in a database. If the user exists in users_db
, their fields should be updated instead of creating a new entry. If the user is not found, a new user should be added with the provided details.
- Define the
add_user
function with parametersname
,age
,role
(default is "user"), andstatus
(default is "active"). - Check if a user with the same
name
exists in theusers_db
list. - If the user exists, update their
age
,role
, andstatus
with the values received as arguments in theadd_user
function. - Return the string
"User {name} updated successfully!"
after updating the user. - If the user doesn't exist, create a new user (
new_user
) with the provided details(name
,age
,role
,status
). - Add the new user (
new_user
) to listusers_db
. - Return the string
"User {name} added successfully!"
after adding the new user.
Solution
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 4