I'm attempting to write a function which takes a list and sums all the numbers in the list, except it ignores sections of the list starting with a list and extending to a 7, but continues to sum after the 7. Here is my code:
def sum67(nums):
i = 0
sum = 0
while i < len(nums):
k = 0
if nums[i] != 0:
sum += nums[i]
i += 1
if nums[i] == 6:
for j in range(i + 1, len(nums)):
if nums[j] != 7:
k += 1
if nums[j] == 7:
k += 2
i += k
Test cases show that 6 and proceeding numbers up until and including 7 are ignored while other numbers are added to the sum, and numbers after the 7 are also added to the sum (as was intended), but for some reason any 7 after the first 7 after a 6 is not summed - this is not what I want and I'm not sure why it's happening. Any suggestions?
Test case results:
[1, 2, 2 Expected: 5. My result: 5 (OK)
[1, 2, 2, 6, 99, 99, 7] Expected: 5. My result: 5 (OK)
[1, 1, 6, 7, 2] Expected: 4 My result: 4 (Chill)
[1, 6, 2, 2, 7, 1, 6, 99, 99, 7] Expected: 2 My result: 1 (Not chill)
[1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7] Expected: 2 My result: 1 (Not chill)
[2, 7, 6, 2, 6, 7, 2, 7] Expected: 18 My result: 9 (Not chill)
`