pos = 1 floor = 0 with open("input", encoding="utf-8") as f: text = f.read() for char in text: if char == '(' or char == ')': if char == '(': floor += 1 else: floor -= 1 if floor == -1: print(f"basement position is {pos}") break; pos += 1