0

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)
4
  • Which part of the code do you think should have thrown an error? Commented Nov 14, 2014 at 8:17
  • if you go out of bounds on a list - you will get an IndexError not ValueError Commented Nov 14, 2014 at 8:17
  • 1
    Also you never reach the body of the while since chunk_size will never be less or equal to 3. Commented Nov 14, 2014 at 8:19
  • @Ankit Jaiswal - "while chunk_size<=len(sequence) and chunk_size>0" should throw an error (I thought) because it evaluates to False. Commented Nov 14, 2014 at 8:20

1 Answer 1

1

It doesn't throw a ValueError, because you raise it within the while block, and the while block is entered only when the required condition of chunk_size being < 0 or > len(str) is not met.

Hence, to correct this, move the error raising part of your code out of the while loop. In fact, your placement of the condition within while is wrong, instead convert it to an if statement. Also, a break before a return statement won't make any sense.

So your code becomes

while True:
    try:
        if chunk_size<=len(sequence) and chunk_size>0:
            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
        else:
            raise ValueError       
    except ValueError:
        print "Oops!  That was no valid number.  Try again..."

Further, instead of raising an error to validate whether the data is in the right format, you can do away with raising the exception itself, and achive the same result with a simple if-else block:

while True:
    if chunk_size<=len(sequence) and chunk_size>0:
        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
    else:
        print "Oops!  That was no valid number.  Try again..."
Sign up to request clarification or add additional context in comments.

1 Comment

@ Mu, and all: I just realized how bloody stupid I was being.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.