Author Topic: [Python] Weird Boolean Logic?  (Read 851 times)

0 Members and 1 Guest are viewing this topic.

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
[Python] Weird Boolean Logic?
« on: 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.
Code: (python) [Select]
>>> 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 -.-
« Last Edit: May 04, 2012, 03:35:18 am by Lionofgod »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: [Python] Weird Boolean Logic?
« Reply #1 on: May 04, 2012, 05:44:51 am »
I'm not a python expert but the "== True" part looks useless.

This should work:
Code: [Select]
if dict.has_key('Bob') :
        print "Bob in dict"

And this should also work: (note the parentheses)
Code: [Select]
if ('Bob' in dict) == True:
        print "Bob is in the dictionary"

But really what I think you should use is:
Code: [Select]
if 'Bob' in dict :
        print "Bob is in the dictionary"
Satan911
Evilzone Network Administrator

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: [Python] Weird Boolean Logic?
« Reply #2 on: May 04, 2012, 02:14:34 pm »
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
« Last Edit: May 04, 2012, 02:15:25 pm by Lionofgod »