title: Placing numbers around bombs in Minesweeper: why do I get "list index out of range"?
body: <p>I have a function that places 'z' amount of bombs randomly scattered across a 10x10 grid. It looks something like this ("b" represents where the bombs are located.) I need to place a number representing how many bombs there are next to the "0" (including diagonals), and I am not sure how to do that.</p>
  
  <pre><code>0 0 0 0 0 0 0 0 b 0
  b 0 0 b 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 b 0 0 0
  0 0 0 b 0 0 0 0 0 0
  0 0 b 0 0 0 0 0 0 b
  0 0 0 0 0 0 0 b 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 b 0 b 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 
  
  from random import*
  mat1 = []
  mat2 = []
  
  def makemat(x):
      for y in range(x):
          list1 = []
          list2 = []
          for z in range(x):
              list1.append(0)
              list2.append("-")
          mat1.append(list1)
          mat2.append(list2)
  makemat(5)
  
  
  
  def printmat(mat):
      for a in range(len(mat)):
          for b in range(len(mat)):
              print(str(mat[a][b]) + "\t",end="")
          print("\t")
  
  
  
  def addmines(z):
      count = 0
      while (count &lt; z):
          x = randrange(0,len(mat1))       
          y = randrange(0,len(mat1))      
          if mat1[y][x] == "b":
              count -= 1
          else:
              mat1[y][x] = "b"
          count += 1
      printmat(mat1)
  addmines(10)
  </code></pre>
  
  <p>This is the function I tried for placing the numbers:</p>
  
  <pre><code>def addscores():
      for x in range(len(mat1)):
          for y in range(len(mat1)):
              if mat1[y][x] != "b":
                  if mat1[y+1][x] == "b":
                      mat1[y][x] = 1 
                  if mat1[y-1][x] == "b":
                      mat1[y][x] = 1                  #...ETC
              else:
                  mat1[y][x] == "b"
  addscores()
  </code></pre>
  
  <p>I keep getting the error <em>list index out of range</em>. How can I resolve this?</p>