-1

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)
  • 1
    It appears you're using `|` which in python is a "bitwise or". You probably meant to use the word "or" instead (i.e. line 4 would be `if i == 1 or i == 2:`). Fix this and tell us if it works – Minion3665 Jan 25 '23 at 13:20
  • Is it because of the [operator precedence](https://docs.python.org/3/reference/expressions.html#operator-precedence). `i==1 | i==2` is equivalent to `( i == (1|i) ) == 2`. – Remi Cuingnet Jan 25 '23 at 13:33

2 Answers2

1

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

DoBaum
  • 41
  • 3
0

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
Remi Cuingnet
  • 943
  • 10
  • 14
Guy
  • 46,488
  • 10
  • 44
  • 88