Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Optional Arguments | Positional and Optional Arguments
Python Functions Tutorial

bookChallenge: Optional Arguments

Task

Swipe to start coding

You are working with a simple user management system. Your goal is to implement a function add_user() that adds new users or updates existing ones in a global list called users_db.

Follow these steps:

  1. Define a function add_user(name, age, role="user", status="active").

  2. Loop through the list users_db to check if a user with the same name already exists.

  3. If the user exists, update their details:

    • "age" β†’ age.
    • "role" β†’ role.
    • "status" β†’ status.

    Then return the message: "User {name} updated successfully!".

  4. If the user doesn’t exist, create a new dictionary named new_user with the provided details (name, age, role, status), append it to users_db, and return: "User {name} added successfully!".

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 4.35

bookChallenge: Optional Arguments

Swipe to show menu

Task

Swipe to start coding

You are working with a simple user management system. Your goal is to implement a function add_user() that adds new users or updates existing ones in a global list called users_db.

Follow these steps:

  1. Define a function add_user(name, age, role="user", status="active").

  2. Loop through the list users_db to check if a user with the same name already exists.

  3. If the user exists, update their details:

    • "age" β†’ age.
    • "role" β†’ role.
    • "status" β†’ status.

    Then return the message: "User {name} updated successfully!".

  4. If the user doesn’t exist, create a new dictionary named new_user with the provided details (name, age, role, status), append it to users_db, and return: "User {name} added successfully!".

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
single

single

some-alt