title: Global variables in recursion. Python
body: <p>OK, i'm using Python 2.7.3 and here is my code:</p>
  
  <pre><code>def lenRecur(s): 
  
      count = 0
  
      def isChar(c):
          c = c.lower()
          ans=''
          for s in c:
              if s in 'abcdefghijklmnopqrstuvwxyz':
                  ans += s
          return ans
  
      def leng(s):
          global count
          if len(s)==0:
              return count
          else:
              count += 1
              return leng(s[1:])
  
      return leng(isChar(s))
  </code></pre>
  
  <p>I'm trying to modify the variable <code>count</code> inside the <code>leng</code> function. Here are the things that I've tried:</p>
  
  <ol>
  <li>If I put the variable count outside the <code>lenRecur</code> function it works fine the first time, but if I try again without restarting python shell, the count (obviously) doesn't restart, so it keeps adding.</li>
  <li>If I change the <code>count += 1</code> line for <code>count = 1</code> it also works, but the output is (obviously) one. </li>
  </ol>
  
  <p>So, my goal here is to get the length of the string using recursion, but I don't know how to keep track of the number of letters. I've searched for information about global variables, but I am still stuck. I don't know if i haven't understood it yet, or if I have a problem in my code. </p>
  
  <p>Thanks in advance!</p>