<div>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.</div>

<div> </div>
<div># father son &quot;who&#39;s your daddy&quot; program<br>#create a dictionary of father son pairs.  Include multiple generations.</div>
<div>pairs = {&quot;Joel&quot; : &quot;Al&quot;,<br>         &quot;Bill&quot; : &quot;Joel&quot;,<br>         &quot;Joseph&quot; : &quot;Bill&quot;,<br>  &quot;Palmer&quot; : &quot;Bob&quot;,<br>         &quot;Bob&quot; : &quot;Will&quot;,<br>
         &quot;Al&quot; : &quot;Royal&quot;}<br>fathers = pairs.keys()<br>sons = pairs.values()</div>
<div>choice = None<br>while choice != &quot;0&quot;:</div>
<div><br>    print(&quot;&quot;&quot;<br>        Welcome to Who&#39;s Your Daddy? A mini database of fathers and sons.<br>        When entering names, please capitilize the 1st letter of each name.<br>        <br>            0 - Exit<br>
            1 - List all Father-Son Pairs<br>            2 - List all Fathers<br>            3 - Look Up Father-Son Pairs<br>            4 - Add a Father-Son Pair<br>            5 - Delete a Father-Son Pair<br>            6 - Replace the Son of a Father-Son Pair<br>
            7 - Replace a Father-Son Pair<br>            8 - Look up Grandfather_Father_Son Pairs<br>        &quot;&quot;&quot;)</div>
<div> </div>
<div>    choice = input(&quot;Choice: &quot;)<br>    <br>    <br>    # To end the program<br>    if choice == &quot;0&quot;:<br>        print (&quot;Thanks for playing.  Goodbye.&quot;)</div>
<div>    # List pairs<br>    elif choice == &quot;1&quot;:<br>        print (&quot;List of all Father and Son pairs: &quot;)<br>        print (pairs)</div>
<div>    elif choice == &quot;2&quot;:<br>        print (&quot;List of all Fathers: &quot;)<br>        for fathers in pairs:<br>            print (fathers)</div>
<div>    elif choice == &quot;3&quot;:<br>        print (&quot;Look up Father-Son pairs: &quot;)<br>        father = input (&quot;What is the name of the father in the pair you want to look up? &quot;)<br>        if father in pairs:<br>
            son = pairs[father]<br>            print (&quot;\n&quot;, father, &quot;is the father of&quot;,son, &quot;.&quot;)<br>        else:<br>            print (&quot;\nSorry.  That father does not exist.&quot;)</div>

<div>    elif choice == &quot;4&quot;:<br>        print (&quot;Add a Father-Son pair: &quot;)<br>        new_father = input (&quot;What is the name of the father you wish to add? &quot;)<br>        if new_father not in pairs:<br>
            new_son = input (&quot;What is the name ofthe son you wish to add? &quot;)<br>            pairs[new_father] = new_son<br>            print (&quot;\n&quot;, new_father, &quot;has been added.&quot;)<br>        else:<br>
            print (&quot;\nThat Father-Son pair already exists.  Try again.&quot;)<br>        <br>    elif choice == &quot;5&quot;:<br>        print (&quot;Delete a Father-Son pair: &quot;)<br>        del_father = input (&quot;What is the name of the father in the pair you wish to delete? &quot;)<br>
        if del_father in pairs:<br>            del pairs[del_father]<br>            print (&quot;\nThe pair in which&quot;, del_father, &quot;is the father has been deleted.&quot;)<br>        else:<br>            print (&quot;\nSorry.&quot;, del_father, &quot;is not a valid father name.&quot;)</div>

<div>    elif choice == &quot;6&quot;:<br>        print (&quot;Replace the son of a Father-Son Pair: &quot;)<br>        son_old = input (&quot;What is the name of the father whose son you wish to replace? &quot;)<br>        if son_old in pairs:<br>
            son_new = input (&quot;What is the name of the new son in this pair? &quot;)<br>            pairs[son_old] = son_new<br>            print (son_new, &quot;has replaced&quot;, son_old, &quot;in the list.&quot;)<br>
        else:<br>            print (&quot;Sorry, that son does not exist.  Try again.&quot;)</div>
<div>    elif choice == &quot;7&quot;:<br>        print (&quot;Replace a Father-Son pair: &quot;)<br>        father = input (&quot;Who is the father in the Father-Son pair do you which to replace? &quot;)<br>        if father in pairs:<br>
            father = input(&quot;What is the name of the father in the new Father-Son pair? &quot;)<br>            son = input(&quot;What is the name of the son in the new Father-Son pair? &quot;)<br>            pairs[father] = son<br>
            print (&quot;\nThe new pair of&quot;, father, &quot;and&quot;, son, &quot;have been added to the list.&quot;)<br>            <br>        else:<br>            print (&quot;Sorry, that father does not exist.  Try again.&quot;)</div>

<div>    elif choice == &quot;8&quot;:<br>        print (&quot;Find the grandson of a person in the list.&quot;)<br>        grandfather = input(&quot;Which father do you want to look up to see if he is a grandfather? &quot;)<br>
        if grandfather in pairs:<br>            father == pairs[grandfather]<br>            for father in pairs:<br>                grandson == pairs[father] <br>                print (grandfather, &quot;is the grandfather of&quot;, grandson, &quot;.&quot;)<br>
        else:<br>            print (&quot;Sorry.&quot;, grandfather, &quot;is not a grandfather.  Try again.&quot;)<br>            <br>        <br>        </div>
<div>input (&quot;\n\nPress the enter key to exit.\n&quot;)<br></div>
<div> </div>
<div>Choice 8 gives me the following error...</div>
<div> </div>
<div>Traceback (most recent call last):<br>  File &quot;C:\Users\Public\Documents\My Python programs\father_son.py&quot;, line 105, in &lt;module&gt;<br>    father == pairs[grandfather]<br>NameError: name &#39;father&#39; is not defined</div>

<div> </div>
<div>I thought father got defined in the </div>
<div> </div>
<div>father == pairs[grandfather]<br></div>
<div>line.  I have tried it a couple different ways but always get the father is not defined error once I enter the name.</div>