[Tutor] Adding items to dictionaries
Jon Moore
jonathan.r.moore at gmail.com
Thu Jan 26 12:19:54 CET 2006
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 Bates
Who is the Father?: Alan Bates
What is the Grand Father?: Master Bates
Traceback (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] = father
KeyError: 'Steven Bates'
>>>
Where am I going wrong?
--
Best Regards
Jon Moore
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060126/98b7144f/attachment.html
More information about the Tutor
mailing list