For the following exercise I am expecting an IndexError for certain test inputs, but it is not occurring.
Goal: Write a function that takes in a list of integers and returns True if it contains 007 in order
My Function:
def spy_game(nums):
for x in range(len(nums)):
print(x)
try:
if nums[x]==0 and nums[x+1]==0 and nums[x+2]==7:
return(True)
except IndexError:
return(False)
Test Cases:
#Case 1
spy_game([1,2,4,0,0,7,5])
#Case 2
spy_game([1,0,2,4,0,5,7])
#Case 3
spy_game([1,7,2,0,4,5,0])
Question:
I included the print statement in the function to try to understand the issue. Case 1 prints 0 1 2 3
and returns True
which is expected. Case 2 prints 0 1 2 3 4 5 6
and does not return anything. Case 3 prints 0 1 2 3 4 5 6
and returns False
. For both Case 2 and 3 I would expect it to print up to 5 and at that point result in an IndexError. I am not sure why Case 2 reaches 6 and has no IndexError and why Case 3 reaches 6 and does have one. Thanks in advance!