我正在尝试检查匹配方程的平衡。下面的代码是我目前拥有的。
#Creating the math equation the use check_html on.
e = "10 - (3 + (2+1)*8)"
def check_html(html_string):
d = Stack()
balanced = True
for symbol in html_string:
if symbol == "(":
d.push(symbol)
elif symbol == ")":
if d.is_empty():
balanced = False
else:
d.pop()
if not d.is_empty:
balanced = False
str1 = "10 - (3 + (2+1)*8)"
str2 = "10 - (3 + (2+1)*8))"
print ("is the first valid?", check_html(str1))
print ("is the second valid?", check_html(str2))
print("Is it balanced? ", balanced)
这段代码的输出是
is the first valid? None
is the second valid? None
Is it balanced? True
应该说第一个是 TRUE,第二个是 FALSE。我现在做错了什么。
冉冉说
相关分类