title: Global statements used within a local function scope
body: <p>I though I was fully aware of how LEGB rule worked until I came across this. Essentially I am to access the &quot;total&quot; variable within my function local namespace. I know the &quot;total variable is a local namespace but for whatever reason when I try to run this function I get the following error:
  UnboundLocalError: local variable 'total' referenced before assignment</p>
  <p>Example:</p>
  <pre><code>total = 0
  
  def two_digit_sum(n):
      for i in str(n):
          total += int(i)
      return total
  
  
  number = 111
  print(two_digit_sum(number))
  </code></pre>
  <p><strong>However</strong> when I utilize the following function , I do not come across the error
  Example:</p>
  <pre><code>def spam():
      print(eggs)
  
  
  eggs = 42
  spam()
  </code></pre>
  <p>^^^
  As you can see the eggs variable is also a global function.</p>