-3

ok so.. i'm trying to write a program that creates a dictionary of son:father entries and another dictionary that contains father:son entries. The program must present the user a menu with five options.

text file is this: john:fred, fred:bill, sam:tony, jim:william, william:mark, krager:holdyn, danny:brett, danny:issak, danny:jack, blasaen:zade, david:dieter, adamLseth, seth:enos

Problem Statement:

Write a program that creates a dictionary of son:father entries and another dictionary that contains father:son entries. Your program must present the user a menu with five options. The following is an example only:


Father/Son Finder

0 – Quit

1 – Find a Father

2 – Find a Grandfather

3 – Find a Son

4 – Find a Grandson


Option 0 ends the program.

Option 1 prompts the user for the name of a son. If the dictionary contains the son:father pair, the program displays the father. Otherwise, the program should tell the user it does not know who the father is.

Option 2 prompts the user for the name of a grandson. If the dictionary contains enough information, the program displays the grandfather. Otherwise, the program should tell the user it does not know who the grandfather is.

Option 3 prompts the user for the name of a father. If the dictionary contains the son:father pair, the program displays the son. Otherwise, the program should tell the user it does not know who the son is.

Option 4 prompts the user for the name of a grandfather. If the dictionary contains enough information, the program displays the grandson. Otherwise, the program should tell the user it does not know who the grandson is.

The program must create the dictionary structure and populate it from data contained in a file provided to you. In addition, the program must continue to ask the user for a menu choice until the user chooses to quit.

I have this thus far. I haven't gotten very far in it...

sons_fathers = {}
fathers_sons = {}
#open filename: names.dat
fo = open("names.dat", "r")
data = fo.read()
print (data)
for line in fo:

here is the flow chart: ![Flow chart][1]

https://jsu.blackboard.com/bbcswebdav/pid-2384378-dt-content-rid-3427920_1/xid-3427920_1

Thanks for the help. I need it lol.

SethMMorton
  • 45,752
  • 12
  • 65
  • 86

3 Answers3

1

How you describe your solution, I don't think a dictionary is what you want for this.

The keys must be unique.

# wont work, keys aren't unique
father_son = {'danny':'brett', 'danny':'issak', 'danny':'jack'} 

You could however try a dictionary with a list as the value:

father_son = {'danny':['brett','issak', 'jack']}
if 'danny' in father_son.keys() and 'brett' in father_son['danny']:
    #do something

Or you could use a list of 2-tuples that stores the pairs:

father_son = [('danny', 'brett'), ('danny', 'issak'), ('danny', 'jack')]
if ('danny', 'brett') in father_son:
    #do something
bcorso
  • 45,608
  • 10
  • 63
  • 75
1

Let's hope nobody give you an exact solution to this homework.

Here some hints, you need to know what you can do with string, string.split() will help you a lot. Also, read about what you can do with dictionary. You will also need the raw_input function

The rest is simple programming. Good luck.

Community
  • 1
  • 1
Nil
  • 2,345
  • 1
  • 26
  • 33
  • 2
    Stackoverflow exists to ask/answer questions, not to make moral judgements about homework. – John La Rooy Nov 21 '13 at 03:40
  • You are right @gnibbler. You and everyone else are free to give him what he asks, but I believe we should not do that. I learned programming by programming, not by copying complete program from SO. Idem for all programmers I know. – Nil Nov 21 '13 at 03:52
0
sons_fathers = {}                  # one father per son

fathers_sons = {}                  # one or many sons per father, use list or 
                                   # set for the values

with open("names.dat", "r") as fo: # use context manager to close file automatically
    for line in fo:                # ?? is there only one line, or one pair per line??
        # do something with line

        # assume you extracted "son" and "father"
        sons_fathers[son] = father
        if father in fathers_sons:
            fathers_sons[father].add(son)
        else:
            fathers_sons[father] = {son}
John La Rooy
  • 295,403
  • 53
  • 369
  • 502