Q:

In Python, why is my for-loop excluding the number before a specific number only when that number is the last in the array?

I am working on a codingbat problem, and I am getting unexpected results when the excluded number is at the end of the array.
def sum13(nums):
  a = []

  if len(nums) == 0:
    return 0
  for i in range(len(nums)):
    if nums[i] == 13 or nums[i-1] == 13:
      continue
    a.append(nums[i])

  return sum(a)

The code should append all numbers in the array to list a and return the sum, excluding any instances of the number 13 and the number immediately following it. It works fine for most of the tests, but when the array ends in 13, it doesn't append the number at index 0 to the list, returning the wrong result.

A:

When i is 0, nums[-1] is evaluated. nums[-1] is the last element of nums. Therein lies your problem.
If nums[-1] is 13, continue is executed, so nothing is appended to nums

