Using boolean in If-Statement in Python

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

thing1 = True thing2 = 3 if thing2 == 3 & thing1: print "hi" 
+4
source share
2 answers

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

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

Since you used & , the error popped up saying:

 TypeError: unsupported operand type(s) for BitAnd: 'bool' and 'number' ^^^^^^ 
+6
source

& is the bitwise AND operator. Instead, you want to use the logical and :

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

Source: https://habr.com/ru/post/1480310/


All Articles