I'm trying to trigger an error when the "chunk_size" is zero, or greater than the length of the "sequence." When I call this function with a chunk_size of 4, and a sequence that equals "123" it doesn't throw an error. What did I do wrong?
def slices(sequence,chunk_size):
position=0
mini_list=[]
answer=[]
while chunk_size<=len(sequence) and chunk_size>0:
try:
for char in sequence:
if len(sequence[position:position+chunk_size])==chunk_size:
mini_seq = sequence[position:position+chunk_size]
for digit in mini_seq:
mini_list.append(int(digit))
answer.append(mini_list)
mini_list=[]
position+=1
return answer
break
except ValueError:
print "Oops! That was no valid number. Try again..."
print slices("012", 4)
IndexErrornotValueErrorwhilesincechunk_sizewill never be less or equal to 3.