计算器的Python代码中出现跟踪错误

问题描述 投票:1回答:1

所以,我正在用python制作计算器(我是相对较新的),经过一番阅读后,我发现了关于re.sub的东西。我遇到的问题是,每当我运行该程序时,它都会在第9行中运行一个Trackback错误。如果有人知道该怎么做,那么任何建议将不胜感激。

import re
#The input should be an equation, for example 26 +(32-1).
a = input("Enter Equation:")
c = "("
#It is supposed to look for parenthesis and make a sub-string out of whats in it
if '(' and ')' in a:
  a = re.sub(r'^[^(]+(','',a)
  print(a)

追踪(最近通话):

File "main.py", line 8, in <module>
a = re.sub(r'^[^(]+(','',a)
missing ), unterminated subpattern at position 6
python regex traceback
1个回答
0
投票

在正则表达式中,您必须转义括号,因为它们具有特殊含义:

a = re.sub(r'^[^(]+\(','',a)
© www.soinside.com 2019 - 2024. All rights reserved.