Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Boolean in an If-Statement in Python

I have some code with an if-statement in it, and one of the conditions is a boolean. However, CodeSkulptor says "Line 36: TypeError: unsupported operand type(s) for BitAnd: 'bool' and 'number'". Please help if you can. This is what that piece of code looks like. (I just changed all the variable names and what the if-statement executes)

thing1 = True
thing2 = 3

if thing2 == 3 & thing1:
   print "hi"
like image 995
Rohan Khajuria Avatar asked Dec 20 '22 05:12

Rohan Khajuria


1 Answers

You want to use logical and (not the &, which is a bitwise AND operator in Python):

if thing2 == 3 and thing1:
   print "hi"

Because you have used &, the error has popped up, saying:

TypeError: unsupported operand type(s) for BitAnd: 'bool' and 'number'
                                           ^^^^^^
like image 175
pradyunsg Avatar answered Jan 01 '23 17:01

pradyunsg