As stated in a previous (but different) question, I’m trying to figure out a ‘simple’ dictionary/database with Python, which will retrieve a name from a list of ten, with the requested information. i.e. input could be ‘John phone’, where the output is ‘John’s phone number is 0401’ (which I have down pat); but I could also input ‘John Birthday’ or ‘John hobbies’ and the output would correspond.
As I’m a complete noob, I’m not even sure where to start. Several hours of googling and poring over lecture notes have yielded nothing so far.
I’ve got the feeling it’s got something to do with the multiple argument % function but our lecturer really wasn’t clear about how to take it further.
So far, what I have is:
#!/usr/bin/python
friends = {'John': {'phone' : '0401',
'birthday' : '31 July',
'address' : 'UK',
'interests' : ['a', 'b', 'c']},
'Harry': {'phone' : '0402',
'birthday' : '2 August',
'address' : 'Hungary',
'interests' : ['d', 'e', 'f']}}
name = raw_input ('Please enter search criteria: ')
if name in friends:
print "%s's phone number is: %s" % (name, friends[name]['phone'])
else:
print 'no data'
I’d also like to use the ‘while’ function so the prog doesn’t close as soon as that information is given, but not sure if this would be appropriate.
Any pointers would be great, even if it’s a ‘try this’ kind of hint, or a link to a relevant website.
Since this is homework, I’ll limit my answer to some hints:
tok = name.split()would splitnameinto a list of words, so'John address'would become['John', 'address'].tok[0]andtok[1], and use them to index intofriendsto get the relevant person and then the relevant field.whileloop so that the user can perform multiple queries. The only thing to figure out is how you’re going to exit that loop.