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

Cursusinhoud

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

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
toggle bottom row

book
Challenge: Optional Arguments

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 4
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt