EvilZone
Programming and Scripting => Scripting Languages => : Lionofgod May 04, 2012, 03:32:51 AM
-
Hello, we started a programming club at school and I was teaching a guy about dictionaries today and we came across something weird.
>>> dict={'John':6,'Bob':3}
>>> if dict.has_key('Bob')==True:
print "hello"
hello
>>> 'Bob' in dict
True
>>> if 'Bob' in dict == True:
print "Bob is in the dictionary"
>>> WHY IT DOESNT SAY BOB IN DICTIONARY!!!!!
'Bob' in dict returns true, why doesnt it print 'Bob is in the
dictionary'
...Code formatting not very readable -.-
-
I'm not a python expert but the "== True" part looks useless.
This should work:
if dict.has_key('Bob') :
print "Bob in dict"
And this should also work: (note the parentheses)
if ('Bob' in dict) == True:
print "Bob is in the dictionary"
But really what I think you should use is:
if 'Bob' in dict :
print "Bob is in the dictionary"
-
Thank you, that worked out : )
Annd your right, the == part is useless, I just wanted to see if it could be used in this case :D