简体   繁体   中英

Break Loop if Index is Out Of Bounds of While Loop in Python

I have a while loop that looks like this:

while a[xp][yp] - a[xp-1][yp] == 0  and a[xp][yp] - a[xp+1][yp] == 0 and a[xp][yp] - a[xp][yp-1] == 0 and a[xp][yp] - a[xp][yp+1] == 0:
    c=randint(0,3)
    if c==0:
        xp=xp+1; yp=yp
    elif c==1:
        xp=xp-1; yp=yp
    elif c==2:
        xp=xp; yp=yp+1
    else:
        xp=xp; yp=yp-1
    xp=xp; yp=yp

The problem is that if xp or yp = 0 or n (the length of the array in either x direction or y, it is a square matrix), then the conditions in the while loop break down and I get an out of bounds error. I would simply like to get a new set of coordinates if xp=0 or xp=n or yp=0 or yp=n (I have a separate piece of code that does this) and let the while loop run again.

The nature of the code seems to be that about 1 in every 4 times the code runs without going out of bounds. I just need it to keep running until it happens to work.

You could simply check if the operation will push the index out of bounds, like so:

if c==0 and xp < len(a)-1:
  xp += 1
elif c==1 and xp > 0:
  xp -= 1
# etc...

This will make sure that xp stays in bounds before actually changing it, rather than looking at it afterwards.


The second problem is in your while statement - even if you make sure that xp and yp are in the bounds for the array, you could be checking outside in your initial condition:

while a[xp][yp] - a[xp-1][yp] == 0 and a[xp][yp] - a[xp+1][yp] == 0  \ 
  and a[xp][yp] - a[xp][yp-1] == 0 and a[xp][yp] - a[xp][yp+1] == 0:

Here, I'll assume that a has a size of 10 by 10 (indexes from 0 to 9). if we set xp to 0 and yp to 9, then this will work out to:

while a[0][9] - a[-1][9] == 0 and a[0][9] - a[1][9] == 0 \
      a[0][9] - a[0][10] == 0 and a[0][9] - a[0][10]:

a[10] will throw the out of bounds error, so you'll have to determine how to change the loop when the index is right on the boundaries of the array. Note that a[9] is still a valid index for the array - it's checking for the next index that is the problem.

As an aside, a[-1] won't actually throw an exception, although it's likely a logic error for you - the negative index will access the final element in the array .


A possible way to fix it, although it's dependent on what you need to do: Python will short-circuit the or operator , so it is possible to write something like this without throwing an exception:

while (xp <= len(a)-2 or a[xp][yp]-a[xp+1][yp] == 0) and \
      (xp > 1         or a[xp][yp]-a[xp-1][yp] == 0) and #etc...

Here, if xp is less than len(a)-2 (the first clause evaluates to true), the other half of the or statement won't be evaluated, the out of bounds exception won't occur, and the loop will continue to run (as long as xp is also greater than 1, and the rest of the statement also evaluates to true).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM