0

So i am writting something that takes a string and then perform a loop on each of that string character. I have an if statement to separate numbers from symbols with a bunch of or operators in condition but it dosen't seem to work.

equation = '2+5-4*6-2/2'

for x in equation:
    if x == ('+' or '-') or ('*' or '/'):
        print('symbol', x)
    else:
        print(x)

The output:

symbol 2
symbol +
symbol 5
symbol -
symbol 4
symbol *
symbol 6
symbol -
symbol 2
symbol /
symbol 2

I actually have a solution, wich is this:

equation = '2+5-4*6-2/2'
symbols = ['+','-','*','/']

for x in equation:
    if x in symbols:
        print('symbol', x)
    else:
        print(x)

But i still want to know why the or operators dont work. I have tried these condition and doesn't work.

if x == ('+' or '-') or ('*' or '/'):
if x == (('+' or '-') or ('*' or '/')):
if x == '+' or '-' or '*' or '/':

I would like to know more, thanks.

0 Answers0