[Tutor] Dictionaries - Using 1 Key:Value to find another

Al Stern albstern at gmail.com
Sun Dec 12 01:10:52 CET 2010


This was another execise in my book.  Following is my code for a program
that uses dictionaries to find and edit pairs of fathers and sons.  The
program works right up to the final step which is to find out if any given
father is actually a grandfather to someone else in the dictionary.  I set
up multiple generations in the dictionary so there would be available
matches.

# father son "who's your daddy" program
#create a dictionary of father son pairs.  Include multiple generations.
pairs = {"Joel" : "Al",
         "Bill" : "Joel",
         "Joseph" : "Bill",
  "Palmer" : "Bob",
         "Bob" : "Will",
         "Al" : "Royal"}
fathers = pairs.keys()
sons = pairs.values()
choice = None
while choice != "0":

    print("""
        Welcome to Who's Your Daddy? A mini database of fathers and sons.
        When entering names, please capitilize the 1st letter of each name.

            0 - Exit
            1 - List all Father-Son Pairs
            2 - List all Fathers
            3 - Look Up Father-Son Pairs
            4 - Add a Father-Son Pair
            5 - Delete a Father-Son Pair
            6 - Replace the Son of a Father-Son Pair
            7 - Replace a Father-Son Pair
            8 - Look up Grandfather_Father_Son Pairs
        """)

    choice = input("Choice: ")


    # To end the program
    if choice == "0":
        print ("Thanks for playing.  Goodbye.")
    # List pairs
    elif choice == "1":
        print ("List of all Father and Son pairs: ")
        print (pairs)
    elif choice == "2":
        print ("List of all Fathers: ")
        for fathers in pairs:
            print (fathers)
    elif choice == "3":
        print ("Look up Father-Son pairs: ")
        father = input ("What is the name of the father in the pair you want
to look up? ")
        if father in pairs:
            son = pairs[father]
            print ("\n", father, "is the father of",son, ".")
        else:
            print ("\nSorry.  That father does not exist.")
    elif choice == "4":
        print ("Add a Father-Son pair: ")
        new_father = input ("What is the name of the father you wish to add?
")
        if new_father not in pairs:
            new_son = input ("What is the name ofthe son you wish to add? ")
            pairs[new_father] = new_son
            print ("\n", new_father, "has been added.")
        else:
            print ("\nThat Father-Son pair already exists.  Try again.")

    elif choice == "5":
        print ("Delete a Father-Son pair: ")
        del_father = input ("What is the name of the father in the pair you
wish to delete? ")
        if del_father in pairs:
            del pairs[del_father]
            print ("\nThe pair in which", del_father, "is the father has
been deleted.")
        else:
            print ("\nSorry.", del_father, "is not a valid father name.")
    elif choice == "6":
        print ("Replace the son of a Father-Son Pair: ")
        son_old = input ("What is the name of the father whose son you wish
to replace? ")
        if son_old in pairs:
            son_new = input ("What is the name of the new son in this pair?
")
            pairs[son_old] = son_new
            print (son_new, "has replaced", son_old, "in the list.")
        else:
            print ("Sorry, that son does not exist.  Try again.")
    elif choice == "7":
        print ("Replace a Father-Son pair: ")
        father = input ("Who is the father in the Father-Son pair do you
which to replace? ")
        if father in pairs:
            father = input("What is the name of the father in the new
Father-Son pair? ")
            son = input("What is the name of the son in the new Father-Son
pair? ")
            pairs[father] = son
            print ("\nThe new pair of", father, "and", son, "have been added
to the list.")

        else:
            print ("Sorry, that father does not exist.  Try again.")
    elif choice == "8":
        print ("Find the grandson of a person in the list.")
        grandfather = input("Which father do you want to look up to see if
he is a grandfather? ")
        if grandfather in pairs:
            father == pairs[grandfather]
            for father in pairs:
                grandson == pairs[father]
                print (grandfather, "is the grandfather of", grandson, ".")
        else:
            print ("Sorry.", grandfather, "is not a grandfather.  Try
again.")



input ("\n\nPress the enter key to exit.\n")

Choice 8 gives me the following error...

Traceback (most recent call last):
  File "C:\Users\Public\Documents\My Python programs\father_son.py", line
105, in <module>
    father == pairs[grandfather]
NameError: name 'father' is not defined

I thought father got defined in the

father == pairs[grandfather]
line.  I have tried it a couple different ways but always get the father is
not defined error once I enter the name.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101211/a64e365f/attachment-0001.html>


More information about the Tutor mailing list