The following code returns "True."
check = [1,2,4,6]
def is_consecutive(a_list):
"""Checks to see if the numbers in a list are consecutive"""
total = 2
while total > 1:
test = a_list.pop(0)
if test == a_list[0] - 1:
total = len(a_list)
return True
else:
return False
break
works = is_consecutive(check)
print(works)
I found a solution, by moving return True to a new block, after the while loop:
check = [1,2,4,6]
def is_consecutive(a_list):
"""Checks to see if the numbers in a list are consecutive"""
total = 2
while total > 1:
test = a_list.pop(0)
if test == a_list[0] - 1:
total = len(a_list)
else:
return False
break
return True
works = is_consecutive(check2)
print(works)
I don't understand fully why moving this piece of code outside the while loop works correctly. It seems to me that once you tell a function to return True, that cannot be changed later on in the function. Is that correct?