[Tutor] getting keyboard input to dictionary (newbie ques)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed, 9 Jan 2002 21:52:27 -0800 (PST)


On Wed, 9 Jan 2002, Frank Holmes wrote:

> 
>   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!

Hi Frank, let's take a look.


> 
>      Def add_name():
       ^^^

You'll want to make that 'Def' lowercased, like this:

###
def add_name():
    print "enter name"
    n=raw_input()
    print "enter number"
    m=raw_input()
    d1[n]=m
###

I can't think of any "technical" reasons why 'def' is case sensitive in
the Python language.  However, this case sensitivity is followed through
everywhere in Python.  Function names, variable names... heck, all names
are case sensitive in Python!  Perhaps for the sake of consistancy, all
the Python keywords are also case sensitive.


One other thing you may find will bite you initially is that variables in
functions are "local" by default.  You probably won't have problems with
add_name(), but it's usually a good idea to label variables as "global" if
their origin is from outside the function.  Here's another version of
add_name() that's a little safer:

###
def add_name():
    global d1
    print "enter name"
    n=raw_input()
    print "enter number"
    m=raw_input()
    d1[n]=m
###

A better approach might be to avoid the global thing altogether, but we
can talk about that later.




> 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'}
>      if x in d1.keys():
>           print d1[x], x
>      if x=='add':
>           add_name()


Looks ok.  Ah, there's one thing you might want to change.  You might want
to move the assignment of d1 out of your while loop.  Can you think of a
reason why it might make a difference?


###
### Version 1
###
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'}
     if x in d1.keys():
          print d1[x], x
     if x=='add':
          add_name()
###


vs.


###
### Version 2
###
d1 = {'a name':'1111', 'another name':'2222', 'A. Name':'1111'}
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
     if x in d1.keys():
          print d1[x], x
     if x=='add':
          add_name()
###



Please feel free to ask questions on Tutor, and we can talk about your
program some more.  Hope this helps!