2

Here is a piece of python code ("result" is a nested list created before)

for i in range(len(result)-1):
    try:
        result[i][3]=0
        result[i+i][0]=0
    except IndexError:
        print "fail", result[i][3], result[i+1][0], i, len(result)
return result

which, to my astonishment, quite often prints "fail" (with non-revealing values for i, len(result), e.g. 24, 31). How can

result[i][3]=0
result[i+i][0]=0

produce the IndexError exception, if

print result[i][3], result[i+1][0]

does not?

0

1 Answer 1

9

You are accessing the index i + i, not i + 1:

result[i+i][0]=0

This means that by the time you reach i // 2 + 1 you have an index error, whatever the size of your list.

Sign up to request clarification or add additional context in comments.

Comments

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.