title: python recursive function error RuntimeError: maximum recursion depth exceeded
body: <p>I have this script:</p>
  <pre><code>def number_of_occurences(c, message):
    position = message.find(c)
    if position == -1:
      return 0
    else:
      if len(message[position:]) == 0:
        return position
      else:
        return position + number_of_occurences(c, message[position:])
  
  number_of_occurences('a', 'azertya')
  </code></pre>
  <p>But when I run it I get this error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;frequency_analysis.py&quot;, line 31, in &lt;module&gt;
      number_of_occurences('a', 'azertya')
    File &quot;file_name.py&quot;, line 29, in number_of_occurences
      return position + number_of_occurences(c, message[position:])
  ...
  ...
  ...
    File &quot;file_name.py&quot;, line 29, in number_of_occurences
      return position + number_of_occurences(c, message[position:])
  RuntimeError: maximum recursion depth exceeded
  </code></pre>
  <p>And I know about this <a href="https://stackoverflow.com/a/8177274/895491">similar question</a> but it didn't help, it took longer time but gives the same error for this:</p>
  <pre><code>sys.setrecursionlimit(10000)
  </code></pre>
  <p>And this:</p>
  <pre><code>sys.setrecursionlimit(30000)
  </code></pre>
  <p>But for this:</p>
  <pre><code>sys.setrecursionlimit(50000)
  </code></pre>
  <p>it gives this error:</p>
  <blockquote>
  <p>Segmentation fault (core dumped)</p>
  </blockquote>
  <p>What am I doing wrong here? thanks in advance.</p>
  <h2>update:</h2>
  <p>Thanks to <a href="https://stackoverflow.com/users/908494/abarnert">@abarnet</a> here is the correct code:</p>
  <pre><code>def number_of_occurences(c, message):
    position = message.find(c)
    nbr = 0.0
    if position == -1:
      return 0
    else:
      nbr += 1
      if len(message[position:]) == 0:
        return nbr
      else:
        return nbr + number_of_occurences(c, message[position + 1:])
  </code></pre>