Challenge: Optional Arguments
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
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain this in more detail?
What are the next steps I should take?
Can you give me an example?
Awesome!
Completion rate improved to 4.35
Challenge: Optional Arguments
Swipe to show menu
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
Thanks for your feedback!
Awesome!
Completion rate improved to 4.35single