dictionaries

Discussion in 'Python' started by Zach Shutters, Sep 10, 2004.

  1. ok, i am trying to learn how to use dictionaries and i am having trouble
    with this code:

    def function1():
    print "function1"

    def function2():
    print "function2"

    dict = {"1":function1,"2":function2}
    x = input ("1 or 2?")

    dict[x]()
     
    Zach Shutters, Sep 10, 2004
    #1
    1. Advertising

  2. Zach Shutters <zshutters <at> comcast.net> writes:
    >
    > def function1():
    > print "function1"
    >
    > def function2():
    > print "function2"
    >
    > dict = {"1":function1,"2":function2}
    > x = input ("1 or 2?")
    >
    > dict[x]()


    Right idea, wrong type. From the docs at:

    http://docs.python.org/lib/built-in-funcs.html

    ] input( [prompt])
    ]
    ] Equivalent to eval(raw_input(prompt)).

    This means that when you use input, it will convert the "1" typed at the
    prompt to the integer 1. So your code should either be:

    >>> d = {1:function1, 2:function2}
    >>> x = input("1 or 2? ")

    1 or 2? 1
    >>> d[x]()

    function1
    >>>


    or

    >>> d = {"1":function1, "2":function2}
    >>> x = raw_input("1 or 2? ")

    1 or 2? 1
    >>> d[x]()

    function1

    You also probably shouldn't name your dictionary 'dict' because then you
    rebind the name 'dict', which is already the builtin 'dict' function.

    Steve
     
    Steven Bethard, Sep 10, 2004
    #2
    1. Advertising

  3. Ok, I get it thanks!

    "Steven Bethard" <> wrote in message
    news:...
    > Zach Shutters <zshutters <at> comcast.net> writes:
    >>
    >> def function1():
    >> print "function1"
    >>
    >> def function2():
    >> print "function2"
    >>
    >> dict = {"1":function1,"2":function2}
    >> x = input ("1 or 2?")
    >>
    >> dict[x]()

    >
    > Right idea, wrong type. From the docs at:
    >
    > http://docs.python.org/lib/built-in-funcs.html
    >
    > ] input( [prompt])
    > ]
    > ] Equivalent to eval(raw_input(prompt)).
    >
    > This means that when you use input, it will convert the "1" typed at the
    > prompt to the integer 1. So your code should either be:
    >
    >>>> d = {1:function1, 2:function2}
    >>>> x = input("1 or 2? ")

    > 1 or 2? 1
    >>>> d[x]()

    > function1
    >>>>

    >
    > or
    >
    >>>> d = {"1":function1, "2":function2}
    >>>> x = raw_input("1 or 2? ")

    > 1 or 2? 1
    >>>> d[x]()

    > function1
    >
    > You also probably shouldn't name your dictionary 'dict' because then you
    > rebind the name 'dict', which is already the builtin 'dict' function.
    >
    > Steve
    >
     
    Zach Shutters, Sep 10, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TJS
    Replies:
    0
    Views:
    421
  2. Replies:
    1
    Views:
    1,593
    Roedy Green
    Jan 9, 2006
  3. Brainwashed

    Keys order in dictionaries

    Brainwashed, Jun 27, 2003, in forum: Python
    Replies:
    2
    Views:
    779
    Duncan Booth
    Jun 27, 2003
  4. lysdexia
    Replies:
    6
    Views:
    560
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    516
    Brandon
    Aug 15, 2008
Loading...

Share This Page