On Thu, Apr 18, 2013 at 11:01 PM, aaB <mecagonoisic...@gmail.com> wrote: > def get_index(thisgen, i): > n = len(thisgen)-1 > cell = thisgen[i] > if i is 0: > print "i==0" > prev, next = thisgen[n], thisgen[i+1] > elif i is n: > print "i==%d" % n > prev, next = thisgen[i-1], thisgen[0] > else: > prev, next = thisgen[i-1], thisgen[i+1] > return prev*4 + cell*2 + next
Without seeing the exception traceback I can't be sure, but here's what I think might be happening: When n == 258, your "i is n" check never happens. Since your protective check for the end of the list doesn't fire, you therefore go into the 'else' clause, and attempt to index thisgen[i+1], which doesn't work. CPython precreates and caches a certain subset of integers, for performance. The exact set depends on the CPython version, but it's low integers only. Within that set, equality is always matched by identity: i = 3 j = i+1 k = j-1 print(i is k) This will most likely print "True" on CPython. But change i to, say, 10000000, and you may find the opposite result. So you can sometimes get away with the dangerous option of testing integers for identity, but the right thing to do is to test for equality. (You even get that right in your debugging messages, using "==" there.) A couple of other tips: > n = len(thisgen)-1 > if i is 0: > prev, next = thisgen[n], thisgen[i+1] You can reference thisgen[-1] instead of thisgen[n] - so you can fold this one into the default case. So all you need to do is deal with the one possibility that i==len(thisgen)-1 and everything else is in the else. > def populate(n): > random.seed() > return [random.randint(0,1) for i in range(n)] Don't reseed your RNG every call; just seed it once and let it run. Seeding the RNG (without using a specific value, which you're not doing here) is done in one of two ways: either from an OS-supplied source of randomness (eg /dev/random), or from the time of day. Reseeding repeatedly from /dev/random is unnecessary and might impact other processes (forcing them to block for lack of available entropy); reseeding from the time of day could mean that every call to populate() returns the exact same sequence of numbers. Have fun! ChrisA -- http://mail.python.org/mailman/listinfo/python-list