title: Global and local variables in Python
body: <p>I am learning Python. A book on Python 3 says the following code should work fine:</p>
  
  <pre><code>def funky():
      print(myvar)
      myvar = 20
      print(myvar)
  
  myvar = 10
  funky()
  </code></pre>
  
  <p>But when I run it in Python 3.3, I got the </p>
  
  <pre><code>UnboundLocalError: local variable 'myvar' referenced before assignment
  </code></pre>
  
  <p>error. My understanding is that the first <code>print(myvar)</code> in <code>funky</code> should be 10 since it's a global variable. The second <code>print(myvar)</code> should be 20 since a local <code>myvar</code> is defined to be 20. What is going on here? Please help clarify.</p>