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
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
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$