First, I am new to Python and using the Python Crash Course by Eric Matthes to learn.
I am somewhat puzzled with the code below. I think I understand that the function build_person() takes in the first and last name and puts them into a dictionary and then the value is returned to the call statement which is assigned to the variable musician.
type or paste code here
def build_person(first_name, last_name):
'''Return a dictionary of information about a person'''
person = {'first': first_name, 'last':last_name}
return person
musician = build_person('jimi', 'hendrix')
print(musician)
This is the result, so I can see the keys and values.
{'first': 'jimi', 'last': 'hendrix'}
What I am missing here is how does the function (what appears to be similar to an append) populate the dictionary, and if the data is in the dictionary why can't I also add the below code after print(musician) and see the keys? I get the error (name 'person is not defined') which leads me to believe that the dictionary is not defined?????
for name in person.keys():
print(name.title())
Thanks, but what I was struggling to understand was how the arguments were also passed as values to the person dictionary.
After staring at the code for a long time it dawned on me that when the arguments are past to the function, they are also the same values in the person dictionary. Don’t know why it took me so long to figure that out.
def build_person(**first_name**, **last_name**):
'''Return a dictionary of information about a person'''
person = {'first': **first_name**, 'last':**last_name**}
return person