Challenge - 5 Problems
Keyword Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of function call with keyword arguments
What is the output of this Python code?
Python
def greet(name, greeting="Hello", punctuation="!"): return f"{greeting}, {name}{punctuation}" print(greet(name="Alice", punctuation=".", greeting="Hi"))
Attempts:
2 left
๐ก Hint
Check how keyword arguments match the function parameters regardless of order.
โ Incorrect
The function greet is called with keyword arguments. The greeting is set to "Hi", punctuation to ".", and name to "Alice". The output string uses these values exactly.
โ Predict Output
intermediate2:00remaining
Function call with missing keyword argument
What is the output of this code snippet?
Python
def calculate_area(length, width=5): return length * width print(calculate_area(width=3, length=4))
Attempts:
2 left
๐ก Hint
Keyword arguments can be passed in any order, but all required arguments must be provided.
โ Incorrect
The function is called with length=4 and width=3. The area is 4 * 3 = 12.
โ Predict Output
advanced2:00remaining
Output when mixing positional and keyword arguments
What will this code print?
Python
def describe_pet(pet_name, animal_type="dog"): return f"I have a {animal_type} named {pet_name}." print(describe_pet("Whiskers", animal_type="cat"))
Attempts:
2 left
๐ก Hint
Positional arguments come first, then keyword arguments can override defaults.
โ Incorrect
The first argument "Whiskers" is assigned to pet_name. The keyword argument animal_type="cat" overrides the default "dog". So the output uses "cat" and "Whiskers".
โ Predict Output
advanced2:00remaining
Error caused by duplicate argument assignment
What error does this code raise?
Python
def multiply(x, y): return x * y print(multiply(2, x=3))
Attempts:
2 left
๐ก Hint
Check if the same argument is given twice by position and keyword.
โ Incorrect
The first argument 2 is assigned to x positionally, then x=3 is passed as keyword argument. This causes a conflict and raises a TypeError about multiple values for 'x'.
๐ง Conceptual
expert2:00remaining
Understanding **kwargs behavior
What is the output of this code?
Python
def show_info(**kwargs): return kwargs.get('name', 'Unknown') + ' is ' + str(kwargs.get('age', 'ageless')) print(show_info(age=30, name='Bob'))
Attempts:
2 left
๐ก Hint
**kwargs collects keyword arguments into a dictionary.
โ Incorrect
The function receives keyword arguments as a dictionary. It gets 'name' and 'age' keys and returns a string combining them. Since both keys are provided, it returns "Bob is 30".