loop to extract info from dictionary

first i am a noob.i am writing a program that will give you relationship info for a child(mother,father,both parents). if you hit 1 it ask you for the childs name and then tell you the childs mothers name,2 same thing except the fathers name,3 will tell you both parents name, and 4 will stop the program. the dictionary is predetermined. this is the general idea

child2parents = {'andrew': {'father': 'john', 'mother': 'jane'}, 'betsy': {'father': 'nigel', 'mother': 'ellen'}, 'louise': {'father': 'louis', 'mother': 'natalie'}, 'chad': {'father': 'joseph', 'mother': 'mary'}}

x = ""

while x != 4:    
choice = int(raw_input("1 = mother, 2 = father 3 = both parents 4 = stop "))
if choice == 1:
    childs_name = raw_input("please enter childs name here ")
    for i in child2parents:
            if i == childs_name:
                print ['i']['mother']

i cant seem to make it work and havent taken the code any further because i cant get option 1 to work


In this case, you don't need to iterate over the dictionary. Once you have the child name, you can use it to access the child's data with child2parents[child_name] or child2parents.get(child_name) (the latter will return None by default if the child_name is not found), e.g. if the user introduces 'andrew' as the child_name, you'll be returned another dictionary {'father': 'john', 'mother': 'jane'}.

So to get the 'mother' key of a child, you can use child2parents[child_name]['mother'].

In Python2.7, if you want to iterate over a dictionary, you can call child2parents.iteritems(), see iteritems.

You're indexing ['i'] by ['mother'] but ['i'] is just a list containing the string "i" and you're not actually indexing the nested dictionary inside child2parents. i is a key inside child2parents upon each iteration. And then you're prompting for a child name which would be this key that you want to further index. I think you mean to use child2parents[i]['mother']

The first error in your program is that you cannot use a string as the index of List( in the print['i']['mother']) you must convert these indeces to integers or think about other ways. I wrote it like this:

child2parents = {'andrew': {'father': 'john', 'mother': 'jane'}, 'betsy': {'father': 'nigel',
                                                                           'mother': 'ellen'}, 'louise': {'father':
                                                                                                          'louis', 'mother': 'natalie'},
                 'chad': {'father': 'joseph', 'mother': 'mary'}}

x = ""

while x != 4:    
 choice = raw_input("Enter what information do you want(mother,father, parents)")
 childs_name = raw_input("please enter childs name here ")
 if choice =="parents":
        print choice+" of "+childs_name+ " is: " + child2parents[childs_name]['father']+","+child2parents[childs_name]['mother']  
        print choice+" of "+childs_name+ " is: " + child2parents[childs_name][choice]

Need Your Help

Decrypting string encrypted in C# with RijndaelManaged class using PHP

php encryption cryptography encryption-symmetric rijndaelmanaged

Here's some C# code (I've modified it slightly to modify some of the hard coded values in it):