Re: [Tutor] Adding items to dictionaries

2006-01-26 Thread Alan Gauld
> pairs[son][0] = father
> KeyError: 'Steven Bates'

> Where am I going wrong?

A KeyError means you are trying to access an object that does not exist
in the dictionary. And this is true, you haven't added anything for Steven 
Bates
yet, but you are trying to access his parents list.
You need to create an associated list which you can then edit.

pairs[son] = [0,0]  # need to put dummy data in
pairs[son][0] = father
pairs[son][1] = grandfather

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding items to dictionaries

2006-01-26 Thread Jon Moore
KentThanks again. I have a question (see below).On 26/01/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
Jon Moore wrote:> Hi,>> I have the following dictionary:>> pairs = {"Jon Moore": ["Tony Moore", "Stanley Moore"],
>  "Simon Nightingale": ["John Nightingale", "Alan Nightingale"],>  "David Willett": ["Bernard Willet", "Robert Willet"],>  "John Jackson": ["John Jackson", "Peter Jackson"],
>  "James Southey": ["Richard Southey", "Paul Southey"],>  "Shaun Forsythe": ["William Forsythe", "Angus Forsythe"],>  "Daniel Geach": ["Mike Geach", "Andy Geach"]}
>> Where the names represent a son, father and grandfather.>> I am trying to add a new term and related definitions to the dictionary,> but my code does not seem to work:>> son = raw_input("Please enter the name of the Son: ")
> if son not in pairs:> father = raw_input("Who is the Father?: ")> grandfather = raw_input("What is the Grand Father?: ")> pairs[son][0] = father
> pairs[son][1] = grandfather> print "\n", son, ",", father, "and" ,grandfather, "have been> added."> else:> print "\nThat Son already exists!"
>> The error I get is:>> pairs[son][0] = father> KeyError: 'Steven Bates'>  > Where am I going wrong?The problem is, when you say   pairs[son][0] = father
pairs[son] does not yet exist. This is on the left side of anassignment, but it is really an access to pair[son]. It is as if you hadwritten   temp = pairs[son]   temp[0] = fatherYou get a KeyError accessing pairs[son].
The solution is to create a new list for the (father, grandfather) pair,and assign that to pairs[son]:ancestors = [father, grandfather]pairs[son] = ancestorsYou might want to rethink how you are storing the data. (father,
grandfather) is actually a (son, father) pair so you might want to storethem as another entry in the dictionary. Also two sons could have thesame father and grandfather; with your scheme you will store the
(father, grandfather) pair twice. In general this kind of duplication ofdata is better avoided.Good point, but I have no idea how to do this! Could you show me? 
You might also want to Google 'python genealogy'.Kent___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor-- Best RegardsJon Moore
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding items to dictionaries

2006-01-26 Thread Kent Johnson
Jon Moore wrote:
> Hi,
> 
> I have the following dictionary:
> 
> pairs = {"Jon Moore": ["Tony Moore", "Stanley Moore"],
>  "Simon Nightingale": ["John Nightingale", "Alan Nightingale"],
>  "David Willett": ["Bernard Willet", "Robert Willet"],
>  "John Jackson": ["John Jackson", "Peter Jackson"],
>  "James Southey": ["Richard Southey", "Paul Southey"],
>  "Shaun Forsythe": ["William Forsythe", "Angus Forsythe"],
>  "Daniel Geach": ["Mike Geach", "Andy Geach"]}
> 
> Where the names represent a son, father and grandfather.
> 
> I am trying to add a new term and related definitions to the dictionary, 
> but my code does not seem to work:
> 
> son = raw_input("Please enter the name of the Son: ")
> if son not in pairs:
> father = raw_input("Who is the Father?: ")
> grandfather = raw_input("What is the Grand Father?: ")
> pairs[son][0] = father
> pairs[son][1] = grandfather
> print "\n", son, ",", father, "and" ,grandfather, "have been 
> added."
> else:
> print "\nThat Son already exists!"
> 
> The error I get is:
> 
> pairs[son][0] = father
> KeyError: 'Steven Bates'
>  >>>
> 
> Where am I going wrong?

The problem is, when you say
   pairs[son][0] = father

pairs[son] does not yet exist. This is on the left side of an 
assignment, but it is really an access to pair[son]. It is as if you had 
written
   temp = pairs[son]
   temp[0] = father

You get a KeyError accessing pairs[son].

The solution is to create a new list for the (father, grandfather) pair, 
and assign that to pairs[son]:

ancestors = [father, grandfather]
pairs[son] = ancestors

You might want to rethink how you are storing the data. (father, 
grandfather) is actually a (son, father) pair so you might want to store 
them as another entry in the dictionary. Also two sons could have the 
same father and grandfather; with your scheme you will store the 
(father, grandfather) pair twice. In general this kind of duplication of 
data is better avoided.

You might also want to Google 'python genealogy'.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Adding items to dictionaries

2006-01-26 Thread Jon Moore
Hi,I have the following dictionary:pairs = {"Jon Moore": ["Tony Moore", "Stanley Moore"], "Simon Nightingale": ["John Nightingale", "Alan Nightingale"],
 "David Willett": ["Bernard Willet", "Robert Willet"], "John Jackson": ["John Jackson", "Peter Jackson"], "James Southey": ["Richard Southey", "Paul Southey"],
 "Shaun Forsythe": ["William Forsythe", "Angus Forsythe"], "Daniel Geach": ["Mike Geach", "Andy Geach"]}Where the names represent a son, father and grandfather.
I am trying to add a new term and related definitions to the dictionary, but my code does not seem to work:    son = raw_input("Please enter the name of the Son: ")    if son not in pairs:
    father = raw_input("Who is the Father?: ")    grandfather = raw_input("What is the Grand Father?: ")    pairs[son][0] = father    pairs[son][1] = grandfather
    print "\n", son, ",", father, "and" ,grandfather, "have been added."    else:    print "\nThat Son already exists!"The error I get is:
Please enter the name of the Son: Steven BatesWho is the Father?: Alan BatesWhat is the Grand Father?: Master BatesTraceback (most recent call last):  File "C:\Documents and Settings\Administrator\Desktop\FOO\transfer\whos_your_daddy_and_grandaddy.py", line 65, in ?
    pairs[son][0] = fatherKeyError: 'Steven Bates'>>> Where am I going wrong?-- Best RegardsJon Moore
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor