0
try:
    def iwillcall():
        def repeat_me():
            print('hello world')
            print(iwillcall())
        print(repeat_me())   
    iwillcall()
except RecursionError:
    print('you reached the limit')

result :

hello world
hello world
hello world
hello world
hello world
hello world
hello world
you reached the limit

How can i count how many times recursion printed 'hello world ' ?

  • Does this answer your question? [Finding the level of recursion call in python](https://stackoverflow.com/questions/12399259/finding-the-level-of-recursion-call-in-python) – Deepan Sep 11 '22 at 02:18
  • can you make a suggestion on the code i wrote because the code in the link is too complicated –  Sep 11 '22 at 02:22

1 Answers1

0

The following solution is using global variable

import sys


print(sys.getrecursionlimit())  # to get maximum recursion of the system you are using
i = 0
try:
    def iwillcall():
        global i
        i = i+1
        def repeat_me():
            global i
            i+=1
            print('hello world')
            print(iwillcall())
        print(repeat_me())
    iwillcall()
except RecursionError:
    print('you reached the limit')
    print(i+2)
Deepan
  • 430
  • 4
  • 13
  • this works perfectly thank you but i wanted to do like enumerate does –  Sep 11 '22 at 09:59