Home > Blockchain >  Why doens t the code get into the if statement? So confused
Why doens t the code get into the if statement? So confused

Time:01-25

it gets in the while loop but never into the if statement...

i = 0
x = ""
while True:
    if i == 1 | i == 2:
        x = x   ('*' * i)   ' '
        i  = 1
        continue
    elif i > 2:
        break
    i  = 1

print(x)

CodePudding user response:

In your if statement you probably want to use the logical "or", i.e. or and not the "bitwise or", i.e. | operator.

Here is an overview of all operators in Python: https://docs.python.org/3/library/operator.html#mapping-operators-to-functions

CodePudding user response:

Because of the operator precedence, with bitwise or you need to use brackets

if (i == 1) | (i == 2)

But you probably want to use logical or

if i == 1 or i == 2
  • Related