title: Python: UnboundLocalError: local variable 'count' referenced before assignment
body: <p>I cannot understand what is the problem in my Python code. It gives me the following error:</p>
  
  <pre><code>    Traceback (most recent call last):
    File "main.py", line 77, in &lt;module&gt;
      main();
    File "main.py", line 67, in main
      count -= 1
  UnboundLocalError: local variable 'count' referenced before assignment
  </code></pre>
  
  <p>Here is part of the code</p>
  
  <p>I defined global variable </p>
  
  <pre><code>count = 3
  </code></pre>
  
  <p>then I created method main</p>
  
  <pre><code>def main():
      f = open(filename, 'r')
  
      if f != None:
          for line in f:
  
              #some code here
  
              count -= 1
              if count == 0: 
                  break
  </code></pre>
  
  <p>what may be wrong here?</p>
  
  <p>Thanks</p>