title: local variable referenced before assignment / Python
body: <p>I'm trying to design a counter which would be incremented each time an action is done. Something like this:</p>
  
  <pre><code>def action_on_accounts(self, accounts):
      for account in accounts[9:]: 
          try:
              self.browser.get(account)
              time.sleep(5)
              action_button = self.browser.find_element_by_xpath(u'//button[contains(@class, "Heart")]').click()
              counter_var = self.count_actions(counter_var)
              print(counter_var)
          except selenium.common.exceptions.NoSuchElementException:
              break
  
  def count_actions(self, counter_var):
      return counter_var + 1
  
  def main(self):
  
      counter_var = 0
      (...)
  </code></pre>
  
  <p>This is throwing a <code>UnboundLocalError: local variable 'counter_var' referenced before assignment</code></p>
  
  <p>I have read that I have to declare <code>counter_var</code> as global inside function and did this:</p>
  
  <pre><code>def count_actions(self, counter_var):
      global counter_var
      return counter_var + 1
  </code></pre>
  
  <p>It was throwing <code>SyntaxError: name 'counter_var' is parameter and global error</code>.</p>
  
  <p>So I tried this :</p>
  
  <pre><code>def count_actions(self):
      global counter_var
      return counter_var + 1
  </code></pre>
  
  <p>Calling it this way :</p>
  
  <pre><code>counter_var = self.count_actions()
  print(counter_var)
  </code></pre>
  
  <p>And now I have <code>NameError: name 'counter_var' is not defined</code>....</p>
  
  <p>Please assist</p>