The documentation string you provided at the top of the module would better be placed in the function, and the examples should be formatted as doctests so that they can automatically be checked. The control flow can be simplified and manual handling of indexes eliminated if you iterate directly over the input list, maintain a `deque` for the currently considered subsequence, and use an inner `while` loop for discarding numbers. from collections import deque def sum_target(numbers, target): '''Given a sequence of positive integers `numbers` and an integer `target`, return whether there is a continuous sub-sequence of `numbers` that sums up to exactly `target`. >>> sum_target([23, 5, 4, 7, 2, 11], 20) # 7 + 2 + 11 = 20 True >>> sum_target([1, 3, 5, 23, 2], 8) # 3 + 5 = 8 True >>> sum_target([1, 3, 5, 23, 2], 7) False ''' subsequence = deque() current_sum = 0 for number in numbers: if current_sum < target: subsequence.append(number) current_sum += number while current_sum > target and subsequence: current_sum -= subsequence.popleft() if current_sum == target: return True return False