Refactor to Use **kwargs
Advanced · Refactor · from Python *args, **kwargs, Default Values, and Keyword Arguments
From the lesson: Python *args, **kwargs, Default Values, and Keyword Arguments
Think about ordering coffee. You always need to pick a size - that's required. But extras like oat milk, an extra shot, or caramel drizzle? Those are optional. You only mention them when you want them.
Read the lessonRefactor the function below to use **kwargs instead of individual parameters. The function should accept name as a required parameter and everything else through **kwargs. Print the name, then print each detail on its own line in key: value format. The output must match exactly.
Original:
def show_student(name, grade, school, gpa):
print(name)
print('grade: ' + str(grade))
print('school: ' + school)
print('gpa: ' + str(gpa))Expected output for show_student('Alice', grade=10, school='Lincoln', gpa=3.8):
Alice
grade: 10
school: Lincoln
gpa: 3.8What Check runs
Your code runs first, then each of these lines. The output must match exactly.
| Runs | Expected output |
|---|---|
| show_student('Alice', grade=10, school='Lincoln', gpa=3.8) | Alice grade: 10 school: Lincoln gpa: 3.8 |
| show_student('Bob', grade=11) | Bob grade: 11 |
More Function Arguments exercises
- Predict the Keyword CallBeginner
- Greeting with a DefaultBeginner
- Sum All NumbersIntermediate
- Fix the Mutable DefaultIntermediate
- Build a Profile StringIntermediate
- Flexible CalculatorAdvanced
All Python exercises · 50 practice problems · Functions quiz