1. 程式人生 > >python 檢測[](){}匹配小程式

python 檢測[](){}匹配小程式

s = input("輸入檢測字串")
pp = True
stack = []
for x in s:
    print(stack)
    if x==')':
       y=stack.pop()
       if y!='(':
           pp = False
print('不匹配')
           break
    elif x==']':
        y = stack.pop()
        if y != '[':
            pp = False
print('不匹配')
            break
    elif 
x=='}': y = stack.pop() if y != '{': pp = False print('不匹配') break elif x in ['(','[','{']: stack.append(x) if pp: print('匹配成功!')