Function with Default Args
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
create_profile("Alice", age=25, city="NYC", job="Engineer"){'name': 'Alice', 'age': 25, 'city': 'NYC', 'job': 'Engineer'}- Inputs:
nameis the one required argument (here"Alice").ageandcityhave fallback values (18 and "Unknown") that apply only when the caller leaves them out. Any other named argument, likejob="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=25andcity="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 that25is 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