import stack def check(string): '''Returns True if the parentheses in a term are balances; False otherwise''' s = stack.Stack() for symbol in string: if symbol == '(': s.push(symbol) elif symbol == ')': if s.isEmpty(): return False else: s.pop() else: # andere Symbole ignorieren pass return s.size() == 0 term = input('Klammerterm mit "(" und ")": ') print(check(term))