Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > dictionaries

Reply
Thread Tools

dictionaries

 
 
Zach Shutters
Guest
Posts: n/a
 
      09-10-2004
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]()




 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      09-10-2004
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

 
Reply With Quote
 
 
 
 
Zach Shutters
Guest
Posts: n/a
 
      09-10-2004
Ok, I get it thanks!

"Steven Bethard" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
TB1.5: Dictionaries =?windows-1252?Q?won=92t_install?= Hendrik Maryns Firefox 10 01-19-2006 04:36 PM
Jazzy and Aspell dictionaries jch.helary@free.fr Java 1 01-09-2006 05:28 PM
tools to create class dictionaries for documentation TJS ASP .Net 0 06-26-2005 08:49 PM



Advertisments