0

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!

4
  • 6
    Because name.lower() will convert Matt to matt and it will not match... Commented Apr 27, 2012 at 7:26
  • 1
    Also, make sure you account for the extra space at the end of the keys: 'Matt '... Commented Apr 27, 2012 at 7:27
  • 1
    Specific information can be gotten just by appending another lookup: friends[name]['phone']. Commented Apr 27, 2012 at 7:52
  • Ok, I tried integrating this into the program just now, in the form of if name in friends: print friends[name]['phone'] How do I make it so that upon entering just a name at the prompt, it gives me all of one persons' info, but if I enter name[address] for example, it provides the address? Commented Apr 27, 2012 at 8:05

4 Answers 4

2

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:

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' ]}
       }

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:

name = raw_input('Please enter search criteria: ')
name = name.strip() # remove whitespace at the beginning and the end of the string
name = name.capitalize() # capitilize the first letter

Of course you can call string functions in a single line:

 name = name.strip().capitalize()

Specific information can be get using friends[name]['<key>'].

Sign up to request clarification or add additional context in comments.

Comments

1

Couple problems here, your keys all have a space at the end like 'Matt '. And they are not lowercase.

1 Comment

@Kay You can create an intermediary lookup dict if you want to keep the correct casing in the keys while still allowing lookups with any case: lookup = dict((key.lower(), key) for key in friends). Now use like this: if name.lower() in lookup: print friends[lookup[name.lower()]].
0

Remove

name = name.lower()

because your dictionary has no keys with all lower case letters or change your dictionary to have all lower case keys.

Comments

0

What you are trying to do is a classical procedural programming with separated data and functions -- the functions are given the data to manipulate them. This leads to complex data structures sometimes.

I suggest to go for object oriented programming in the case. Write a class for a person (it can be dictionary based to capture whatever information).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.