Problem No 8
Write a function that handles keyword arguments
Easy≈ 8 minute session
Lesson guide
What this Python exercise practices
Write a function that handles keyword arguments is a beginner practice lesson that focuses on functions, parameters, return values. It is designed to be solved in about 8 minutes with examples, starter code, and test feedback.
Prerequisites
- Python variables
- Function parameters
- Return values
Difficulty and time
- Level
- Beginner
- Estimated time
- 8 minutes
Practice path
Summary
Use **kwargs to accept arbitrary keyword arguments and process them.
Problem statement
Implement count_kwargs(**kwargs) which accepts any number of keyword arguments and returns an integer count of those keyword arguments. The function should work when called with zero or many keyword arguments.
Task
Create a function count_kwargs(**kwargs) that returns how many keyword arguments were passed.
Examples
Count two keyword args
Input
count_kwargs(name='Alice', active=True)
Output
2
Explanation
Two keyword arguments (name and active) were provided.
Input format
Zero or more keyword arguments in the form key=value.
Output format
An integer representing the number of keyword arguments passed.
Constraints
Do not use external libraries. The order of kwargs is not important; only the count matters.
Samples
Sample input 0
count_kwargs()
Sample output 0
0
Explanation 0
No keyword arguments, so the count is 0.
AI assistant
Ask me anything!
Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.
Chat history is temporary and will not be saved.
Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.