title: Python global variable confusion
body: <p>This is my code, I am confused why that I got the exception <code>local variable &quot;final&quot; referenced before assignment</code>. I have tried to declare it as global in <code>dfs</code> function but still no help. I am wondering why that <code>vistited</code> is correctly referenced by <code>dfs</code> function while <code>final</code> is not.</p>
  <pre><code>def movingCount(self, m, n, k):
          def calculate(x, y):
              x_str = str(x)
              y_str = str(y)
              result = 0
              for i in x_str:
                  result += int(i)
              for j in y_str:
                  result += int(j)
              return result
          vistited = [[0]*n for _ in range(m)]
          final = 0
          def dfs(x, y):
              print(vistited, final)
              if x == m or y == n or calculate(x, y) &gt; k:
                  return
              else:
                  vistited[x][y] = 1
                  final += 1
              dfs(x+1, y)
              dfs(x, y+1)
          dfs(0, 0)        
          return final
  </code></pre>