Editor

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 lesson

Refactor 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.8
def show_student(name, grade, school, gpa):
print(name)
print('grade: ' + str(grade))
print('school: ' + school)
print('gpa: ' + str(gpa))
show_student('Alice', grade=10, school='Lincoln', gpa=3.8)

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected 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

All Python exercises · 50 practice problems · Functions quiz