[Tutor] Calling a dictionary entry inside of a function
Alan Gauld
alan.gauld at btinternet.com
Sat Sep 19 03:32:32 CEST 2009
Corey Richardson wrote:
> I am trying to use a parameter of a function to call a word inside a
> dictionary.
> Here is my code
> wordList = {
> 'Apple' : ["A delicious snack"],
> 'Word' : ["This code is not working..."],
> }
> def define(word):
> print wordList['Word']
>
You put quotess around word which means Pythn uses the literal string
'word' as te kwey. You want to use thevalue of the variable word so you
should just use word without quotes. Understanding the difference
between literal values and symbolic values(variables) is very important.
here is a slightly differemt example:
d = {1:'first', 2:'second'}
one=0
two=2
print d[1] # ok
print d[one] # not ok, looking for d[0]
print d[2] # ok
print d[two] # ok lokking for d[2]
print d['two' # not ok, looking for d['two']
HTH,
Alan G.
More information about the Tutor
mailing list