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

Kurssisisältö

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

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
toggle bottom row

book
Challenge: Optional Arguments

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 4
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt