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
course content

Course Content

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
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.

  1. Define the add_user function with parameters name, age, role (default is "user"), and status (default is "active").
  2. Check if a user with the same name exists in the users_db list.
  3. If the user exists, update their age, role, and status with the values received as arguments in the add_user function.
  4. Return the string "User {name} updated successfully!" after updating the user.
  5. If the user doesn't exist, create a new user (new_user) with the provided details(name, age, role, status).
  6. Add the new user (new_user) to list users_db.
  7. Return the string "User {name} added successfully!" after adding the new user.

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
toggle bottom row

book
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.

  1. Define the add_user function with parameters name, age, role (default is "user"), and status (default is "active").
  2. Check if a user with the same name exists in the users_db list.
  3. If the user exists, update their age, role, and status with the values received as arguments in the add_user function.
  4. Return the string "User {name} updated successfully!" after updating the user.
  5. If the user doesn't exist, create a new user (new_user) with the provided details(name, age, role, status).
  6. Add the new user (new_user) to list users_db.
  7. Return the string "User {name} added successfully!" after adding the new user.

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
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt