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

col speed ajarncolin at gmail.com
Sun Dec 12 01:34:05 CET 2010


On 12 December 2010 07:10, Al Stern <albstern at gmail.com> wrote:

> 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.
>
>     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.
>
> I'm sure somebody has beaten me to it, but:
You have used "==" rather than "="
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101212/6966cb51/attachment.html>


More information about the Tutor mailing list