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

Svep för att visa menyn

book
Challenge: Positional Arguments

Uppgift

Swipe to start coding

Implement the register_user function that checks the user's age, adds their details to a database (users_db), and returns a success or failure message.

  1. Define the function register_user that takes parameters username, email, and age.
  2. Inside the function register_user, check if age is less than 18. If it is, return the message "Registration failed: age must be 18 or older."
  3. Create a dictionary user with the keys username, email, and age, and assign the corresponding values.
  4. Add the user dictionary to the users_db list using the appropriate method.
  5. If everything is successful, return the message "User {username} registered successfully!", where {username} is the actual username.
  6. Call the register_user function with example parameters for a user, either by passing arguments directly or as a dictionary.

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 2
Vi beklagar att något gick fel. Vad hände?

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

book
Challenge: Positional Arguments

Uppgift

Swipe to start coding

Implement the register_user function that checks the user's age, adds their details to a database (users_db), and returns a success or failure message.

  1. Define the function register_user that takes parameters username, email, and age.
  2. Inside the function register_user, check if age is less than 18. If it is, return the message "Registration failed: age must be 18 or older."
  3. Create a dictionary user with the keys username, email, and age, and assign the corresponding values.
  4. Add the user dictionary to the users_db list using the appropriate method.
  5. If everything is successful, return the message "User {username} registered successfully!", where {username} is the actual username.
  6. Call the register_user function with example parameters for a user, either by passing arguments directly or as a dictionary.

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 2
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