title: IndexError: list index out of range when trying to make grid for minesweeper in python
body: <p>I'm trying to make minesweeper grid in python. So basically, first there are only zeros and 10 bombs (X's) in the grid and if some index in this grid has bomb in it, the program should add 1 to all surrounding indexes, excluding indexes that also have bomb in it.</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-py prettyprint-override"><code>import random
  
  board1 = [[0] * 9 for n in range(9)]
  for pos in random.sample(range(72), 10):
      a = pos // 9
      b = pos % 9
      board1[a][b] = "X"
  
      if a != 0 and b != 0 and board1[a-1][b-1] != "X":
          board1[a - 1][b - 1] += 1
  
      if a != 0 and board1[a-1][b] != "X":
          board1[a - 1][b] += 1
  
      if a != 0 and b != 9 and board1[a-1][b+1] != "X":
          board1[a - 1][b + 1] += 1
  
      if b != 0 and board1[a][b-1] != "X":
          board1[a][b - 1] += 1
  
      if b != 9 and board1[a][b+1] != "X":
          board1[a][b + 1] += 1
  
      if a != 9 and b != 0 and board1[a+1][b-1] != "X":
          board1[a+1][b - 1] += 1
  
      if a != 9 and board1[a+1][b] != "X":
          board1[a + 1][b] += 1
  
      if a != 9 and b != 9 and board1[a+1][b+1] != "X":
          board1[a + 1][b + 1] += 1
  
  
  frmt = "{:&gt;3}"*len(board1)
  for row in board1:
      print(frmt.format(*row))
  </code></pre>
  
  <p>The code seems to work, but sometimes I get this error:</p>
  
  <pre><code>if a != 0 and b != 9 and board1[a-1][b+1] != "X":
  IndexError: list index out of range
  </code></pre>
  
  <p>The error usually says <code>if a != 0 and b != 9 and board1[a-1][b+1] != "X":</code>, but sometimes it might give error from some other line, like: <code>if b != 9 and board1[a][b+1] != "X":</code>.</p>
  
  <p>I've been pulling my hair out for days with this, can someone help?</p>