[Tutor] getting keyboard input to dictionary (newbie ques)
Andy W
toodles@yifan.net
Thu, 10 Jan 2002 14:05:00 +0800
Hi
>
> I am writing a simple phone number database type program. I am using
> dictionaries to handle variations in the way the names can be entered ie:
> jones:555-5555, Jones:555 5555, etc.
> I can get it to return names or quit in response to keyboard input from
a
> menu, but cannot figure out how to add entries to the dictionary from
there.
> Help!
>
> Def add_name():
> print "enter name"
> n=raw_input()
> print "enter number"
> m=raw_input()
> d1[n]=m
>
> while 1:
> print "some greeting"
> print "type name to search"
> print "type 'add' to add names or 'quit' to exit"
> x=raw_input()
> if x=='quit':
> break
> d1={'a name':'1111', 'another name':'2222', 'A. Name':'1111'}
^^^^^^^^
Here you are initialising the dictionary each time you pass through the
loop, therefore destroying that record you just input.
To get around this, just put d1=... above the while loop, and it'll
initialise it only once.
> if x in d1.keys():
> print d1[x], x
> if x=='add':
> add_name()
>
>
> It will let me retrieve and quit, and when I enter 'add' it will print
> as if it is working but the entries are not added to the dict.
> I have tried converting the raw-input() value to a string using the
> str.() then using that identity in the d1[n]=m but still won't update.
> I'm missing something obvious... help.
>
>
>
>
>
> _________________________________________________________________
> Chat with friends online, try MSN Messenger: http://messenger.msn.com
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>