title: "UnboundLocalError: local variable referenced before assignment" when incrementing variable in function
body: <p>I am getting this error and I've read other posts but they say to put <code>global</code> before <code>dollars = 0</code> which produces a syntax error because it doesn't allow the <code>= 0</code>. I'm using <code>dollars</code> as a counter so I can keep track of what I add to it and display it back when needed.</p>
  <pre><code>dollars = 0
  
  def sol():
      print('Search or Leave?')
      sol = input()
      if sol == 'Search':
          search()
      if sol == 'Leave':
          leave()
  
  def search():
      print('You gain 5 bucks')
      dollars = dollars + 5
      shop()
  
  def leave():
      shop()
  
  def shop():
      shop = input()
      if shop == 'Shortsword':
          if money &lt; 4:
              print('I\'m sorry, but you don\'t have enough dollars to buy that item.')
              shop1()
          if money &gt; 4:
              print('Item purchased!')
              print('You now have ' + dollars + ' dollars.')
  
  sol()
  </code></pre>
  <p>Error message:</p>
  <pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
    File &quot;C:/Users/justin/Python/Programs I Made/Current/Testing.py&quot;, line 29, in &lt;module&gt;
      sol()
    File &quot;C:/Users/justin/Python/Programs I Made/Current/Testing.py&quot;, line 7, in sol
      search()
    File &quot;C:/Users/justin/Python/Programs I Made/Current/Testing.py&quot;, line 13, in search
      dollars = dollars + 5
  UnboundLocalError: local variable 'dollars' referenced before assignment
  </code></pre>