I’m currently learning the basics in Python, and I need to create a name database of ten people, including other details (phone number, address, birthday, and three interests).
The dream is that upon entering a persons name at the prompt, all of their details will appear. Or upon entering name['phone'], the persons name and phone number will come up. Same for address, birthday, and interests. I’ve been told that I have to able to get the name and relevant field (phone, hobbies, etc) from the command line, using the argv list, but I’m not sure how to do this. Any help would be much appreciated!
Here is an example of what I’ve written up for the dictionary:
friends = {'Matt ' : {'phone' : '13579',
'birthday' : '2 Dec',
'address' : 'Sydney',
'interests' : ['a', 'b', 'c' ]},
'Tim ' : {'phone' : '24680',
'birthday' : '19 Feb',
'address' : 'Honolulu',
'interests' : ['x', 'y', 'z' ]},
'Kate ' : {'phone' :'12345',
'birthday' : '30 Jun',
'address' : 'Beijing',
'interests' : ['q', 'w', 'e' ]}
}
name = raw_input ('Please enter search criteria: ')
if name in friends:
print 'yes'
else:
print 'no data'
Thank you kindly!
First of all, you must have your dictionary keys in a consistent style. I mean, either all of them mights start with a capital or all of them might be upper case or lower case. Also, having whitespace in the beginnning or the end of a key is not a good choice.
Let’s assume you have following key structure:
Since all the keys start with a capital letter and have no white space at the beginning or the end, you can do the following:
Of course you can call string functions in a single line:
Specific information can be get using
friends[name]['<key>'].