[Tutor] Help with excetion handing and making my code more efficient needed please

Rikard Bosnjakovic rikard.bosnjakovic at gmail.com
Fri May 18 23:49:52 CEST 2007


On 5/18/07, Matt Smith <matt at mattanddawn.orangehome.co.uk> wrote:

> I am not sure
> how to suppress or avoid this error so that neighbour_count is not
> incremented for indexes outside the matrix.

Something like this:

try:
   the_index_outside_matrix_test()
except IndexError:
  suppress_the_error()

> Is there a better way of doing this?

Perhaps something like this:

for n in (x, x+1, x-1):
  for m in (y, y+1, y-1):
    if matrix[n, m]:
      neighbour_count = neighbour_count + 1



-- 
- Rikard - http://bos.hack.org/cv/


More information about the Tutor mailing list