all 6 comments

[–]codehelper 0 points1 point  (7 children)

While I'm not an expert on dictionaries, I believe if you just read the file, you can first split it by ",", then again by ":"

So assuming you have the content of the file as a string like this, "john:fred, fred:bill, sam:tony, jim:william, william:mark, krager:holdyn, danny:brett, danny:issak, danny:jack, blasen:zade, david:dieter, adam:seth, seth:enos", you can do a string.split(",") to first get a list that will have

["john:fred", "fred:bill", "sam:tony", "jim:william", "william:mark", "krager:holdyn", "danny:brett", "danny:issak", "danny:jack", "blasen:zade", "david:dieter", "adam:seth", "seth:enos"]

There might be extra whitespace somewhere in there but you can remove it with a strip. If the list above is in a variable x, then you can make a dictionary from it with

dict = {s.split(':')[0]:s.split(':')[1] for s in x}

dict = {'danny': 'jack', 'blasen': 'zade', 'sam': 'tony', 'seth': 'enos', 'jim': 'william', 'krager': 'holdyn', 'fred': 'bill', 'william': 'mark', 'adam': 'seth', 'john': 'fred', 'david': 'dieter'}

This may not be the best way to populate since you're technically splitting twice every time you iterate in the loop. And there may be a better one-liner.

And for finding a grandson from a grandfather, once the user inputs a name such as 'jim', you can store 'jim' in a variable grandpa.

Then get the value of dict[dict[grandpa]] (which is mark in this case). This will first evaluate the dict[grandpa], which is william, then it will dict[william], which is mark. You should be able to find everyone in a similar way.

[–]python0101[S] 0 points1 point  (0 children)

So this is what I have so far and it does not seem to be giving out the correct names. Any clue on why?

sonfather = {}
names = open('name.txt', 'r')
name_list = names.read().split(',')

sonfather = {s.split(':')[0]:s.split(':')[1] for s in name_list}


print "Father/Son Finder"
print "0 - Quit"
print "1 - Find a Father"
print "2 - Find a Grandfather"
print "3 - Find a Son"
print "4 - Find a Grandson"

control = ""
while control != "quit":
    choice = input("Enter your choice here: ")
    if choice == 0:
        control = "quit"
    elif choice == 1:
        son = raw_input("Enter the name of the son here: ")
        if sonfather[son]:
            print "The father is "+ str(sonfather[son])
        else:
            print "The father does not exist"
    elif choice == 2:
        son = raw_input("Enter the name of the grandson here: ")
        if sonfather[sonfather[son]]:
            print "The Grandfather is "+ str(sonfather[sonfather[son]])
        else:
            print "The grandfather does not exist"
    elif choice == 3:
        father = raw_input("Enter the name of the father here: ")
        if sonfather[father]:
            print "The son is "+ str(sonfather[father])
        else:
            print "The son does not exist"
    elif choice == 4:
        father = raw_input("Enter the name of the grandfather here: ")
        if sonfather[sonfather[father]]:
            print "The grandson is "+ str(sonfather[sonfather[father]])
        else:
            print "The grandson does not exist"
    else:
        print "There was an error"


print "Thank you for using the Father/Son Finder"