def func(nums):
i, t, s = 0, -6, 0 # index, temp, sum
while i < len(nums):
try:
t = nums.index(13, t + 6) # index -> valueError
s += sum(nums[i:t])
i = t + 6
except ValueError:
return s + sum(nums[i:])
return s
# use pytest
def test_func():
assert func([]) == 0
assert func([1, 2, 3]) == 6
assert func([1, 2, 13, 4, 5, 6, 7, 8, 9]) == 12
assert func([1, 2, 13, 4]) == 3
知识点
>>> help(list.index)
Help on method_descriptor:
index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.