-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathfunction-with-variable-length-keyword-arguments-(*kwargs).py
More file actions
41 lines (33 loc) · 1.69 KB
/
function-with-variable-length-keyword-arguments-(*kwargs).py
File metadata and controls
41 lines (33 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'''
Function with variable-length keyword arguments (**kwargs)
100xp
Let's push further on what you've learned about flexible arguments - you've used *args,
you're now going to use **kwargs! What makes **kwargs different is that it allows you to
pass a variable number of keyword arguments to functions. Recall from the previous video
that, within the function definition, kwargs is a dictionary.
To understand this idea better, you're going to use **kwargs in this exercise to define a
function that accepts a variable number of keyword arguments. The function simulates a
simple status report system that prints out the status of a character in a movie.
Instructions
-Complete the function header with the function name report_status. It accepts a single
flexible argument **kwargs.
-Iterate over the key-value pairs of kwargs to print out the keys and values, separated
by a colon ':'.
-In the first call to report_status(), pass the following keyword-value pairs: name="luke",
affiliation="jedi" and status="missing".
-In the second call to report_status(), pass the following keyword-value pairs: name="anakin",
affiliation="sith lord" and status="deceased".
'''
# Define report_status
def report_status(**kwargs):
"""Print out the status of a movie character."""
print("\nBEGIN: REPORT\n")
# Iterate over the key-value pairs of kwargs
for key, value in kwargs.items():
# Print out the keys and values, separated by a colon ':'
print(key + ": " + value)
print("\nEND REPORT")
# First call to report_status()
report_status(name='luke', affiliation='jedi', status='missing')
# Second call to report_status()
report_status(name='anakin', affiliation='sith lord', status='deceased')