v9.1.1
Menu

Function with Default Args

easyFREEFunctionsPython FunctionsPython Foundations

Problem Statement

Create functions with default arguments and keyword arguments.

Background

Python functions support:

  • Default arguments: def f(a, b=10)
  • Keyword arguments: f(a=1, b=2)
  • *args: Variable positional arguments
  • **kwargs: Variable keyword arguments

Your Task

Write a function **create_profile(name, age=18, city="Unknown", extra) that creates a user profile.

Output Format

Return a dictionary with all provided information:

  • "name": The name
  • "age": The age (default 18)
  • "city": The city (default "Unknown")
  • Plus any extra keyword arguments

Example

Input
create_profile("Alice", age=25, city="NYC", job="Engineer")
Output
{'name': 'Alice', 'age': 25, 'city': 'NYC', 'job': 'Engineer'}
Reasoning
  • Inputs: name is the one required argument (here "Alice"). age and city have fallback values (18 and "Unknown") that apply only when the caller leaves them out. Any other named argument, like job="Engineer", has no matching parameter, so it is collected into the extra-fields parameter, a mapping from field name to value.
  • Filling the fixed fields: the caller supplied age=25 and city="NYC", so both defaults are replaced: name → 'Alice', age → 25, city → 'NYC'.
  • Adding the extras: the extra fields hold exactly one pair, job → 'Engineer', which becomes a fourth entry after the three fixed ones.
  • Output: a dictionary with 4 keys in this order: {'name': 'Alice', 'age': 25, 'city': 'NYC', 'job': 'Engineer'}. Note that 25 is still an integer, not a string.
  • Notice: with only the name "Bob" passed, both defaults show up and there are no extras, so the result has exactly 3 keys: {'name': 'Bob', 'age': 18, 'city': 'Unknown'}.

Constraints

  • name is required
  • age defaults to 18
  • city defaults to "Unknown"
  • Include any extra keyword arguments

Learn this in

More from Python Foundations

solution.py

Test results

Sign in to run your code against the test cases.