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

Kursinnehåll

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

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
toggle bottom row

book
Challenge: Optional Arguments

Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 4
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt