variables declared outside function
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/855493/referenced-before-assignment-error-in-python">referenced before assignment error in python</a><br>
  <a href="http://stackoverflow.com/questions/8934772/local-var-referenced-before-assignment">local var referenced before assignment</a>  </p>
</blockquote>



<p>Ok, i was just trying some to see how variables scopes work and ran in the following situation. All ran form terminal:</p>

<pre><code>  x = 1
  def inc():
      x += 5

  inc()
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "&lt;stdin&gt;", line 2, in inc
  UnboundLocalError: local variable 'x' referenced before assignment
</code></pre>

<p>So I was thinking well maybe I don't have access to x in my method, so I tried:</p>

<pre><code> def inc():
    print x

 1
</code></pre>

<p>So this works. Now I know I could just do: </p>

<pre><code> def inc():
     global x
     x += 1
</code></pre>

<p>And this would work. But my question is why does the first example fail. I mean I would expect since <code>print x</code> worked that x is visible inside the function so why would the x += 5 fail ?</p>
