-2
num = input('insert:')
if '/' or '*' or '+' or '-' in num:
    sp = num.split('*' or '+' or '/' or '-')
    sume = 0
    for be in sp:
        sume +=int(be)
        print(sume)

Why this code does not convert to this and gives this error

kindall
  • 178,883
  • 35
  • 278
  • 309
  • `if '/' or '*' or '+' or '-' in num` That is not the right way to check multiple values. See https://stackoverflow.com/q/15112125/494134 – John Gordon Feb 19 '22 at 04:08
  • try `print('*' or '+' or '/' or '-')` to see what it's actually splitting on – kindall Feb 19 '22 at 04:08
  • Does this answer your question? [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value) – BrokenBenchmark Feb 19 '22 at 06:09

1 Answers1

1

Because 'or' doesn't work that way. or just joins two boolean expressions. You need something like:

import re

num = input('insert:')
parts = re.match( r"(\d*)([-+*/])(\d*)",num)
if not parts:
    print("No operator found.")

num1 = int(parts[1])
num2 = int(parts[3])
if parts[2] == '+':
    ans = num1 + num2
elif parts[2] == '-':
    ans = num1 - num2
elif parts[2] == '*':
    ans = num1 * num2
elif parts[2] == '/':
    ans = num1 / num2
print(ans)

This one actually does the correct operation. No error handling included. Output:

timr@tims-gram:~/src$ python x.py
insert:3+4
7
timr@tims-gram:~/src$ python x.py
insert:3*4
12
timr@tims-gram:~/src$
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30