正则表达式
import re
def cal(chars):
chars = chars.replace("(","").replace(")","")
op, a, b = chars.split()
a, b = int(a), int(b)
if op=="mul":
c = a*b
return str(c)
if op=="div":
if b==0:
return "error"
else:
c = a//b
return str(c)
if op=="sub":
c = a-b
return str(c)
if op=="add":
c = a+b
return str(c)
s = input()
arr = re.findall("\(\w+ -?\d+ -?\d+\)", s)
flag = True
while arr and flag:
for i in arr:
c = cal(i)
if c=="error":
print("error")
flag = False
break
s = s.replace(i, c)
arr = re.findall("\(\w+ \d+ \d+\)", s)
if flag:
print(s)