-2

So, I am making a script that basically adds up, then converts the number to ascii. It works a little bit, but after a while of running I get the error: maximum recursion depth exceeded while calling a Python object. I do not understand what it means at all... Here's my code:

letter1=0
letter2=0
letter3=0
def first():
    global letter1
    letter1+=1
    print(chr(letter1), chr(letter2), chr(letter3))
    if letter1==127:
        second()
    else:
        first()
def second():
    global letter1
    global letter2
    letter2+=1
    print(chr(letter1), chr(letter2), chr(letter3))
    letter1=0
    if letter2==127:
        third()
    else:
        first()
def third():
    global letter1
    global letter2
    global letter3
    letter1=0
    letter2=0
    letter3+=1
    print(chr(letter1), chr(letter3), chr(letter3))
    if letter3==127:
        print("\n\n\n\n\n\n\n\n\n\n")
    else:
        first()
first()
user3556962
  • 353
  • 2
  • 4
  • 4
  • There are no inputs, and the error is very long – user3556962 Sep 29 '14 at 15:12
  • Are you trying to `print` every combination of up to three characters with ASCII value below 128? That's 2,113,664 combinations - recursion is a very poor choice of approach. Python limits the depth of recursive calls (to 1,000 by default), hence the error you are seeing. – jonrsharpe Sep 29 '14 at 15:13
  • "I get the error: maximum recursion depth exceeded while calling a Python object. I do not understand what it means at all." - Are you aware of a site called google.com where you can search for things on the internet? Because you can search for the exact error message and get thousands of results that explain it. Sarcasm aside, we expect people to do some research before asking a question on Stack Overflow. Please use google and the Stack Overflow search before asking a question; your error is by no means uncommon and it is documented extensively what your error message means. – l4mpi Sep 29 '14 at 15:26

2 Answers2

3

You are exceeding the recursion depth limit. To change the depth limit see this question: Python: Maximum recursion depth exceeded. However, even that will not help you, since the amount of calls that you are making is very large. Before each of your functions is finished it is calling another (or itself) so this is what makes your program recursive. Each time a function is called, the function along with its arguments are added to the computer's stack in the form of a frame. Each time you return from a function the frame is removed from the stack. So, when you call another function before returning then you've increased the depth of the stack (recursion depth) by 1. Side note: Python will automatically return None at the end of your functions even if you do not specify a return statement.

The function first makes your recursion depth 128 (large but still functional). Adding the second function increases the depth to (128*128 or 16384) in order for this to complete you must increase the recursion depth. Try this bit of code and you will notice it stops when letter3 does it's first increase. It may not even get to the first increase of letter3 depending on your computer, which will give you an idea of how many function calls you have (this is very fitting for the title of this website, stack overflow).

Btw global variables are not recommended so I changed that for you.

import sys
sys.setrecursionlimit(16385)

def first(letter1, letter2, letter3):
    letter1+=1
    print(chr(letter1), chr(letter2), chr(letter3))
    if letter1==127:
        second(letter1, letter2, letter3)
    else:
        first(letter1, letter2, letter3)

def second(letter1, letter2, letter3):
    letter2+=1
    print(chr(letter1), chr(letter2), chr(letter3))
    letter1=0
    if letter2==127:
        third(letter1, letter2, letter3)
    else:
        first(letter1, letter2, letter3)

def third(letter1, letter2, letter3):
    letter1=0
    letter2=0
    letter3+=1
    print(chr(letter1), chr(letter3), chr(letter3))
    if letter3==127:
        print("\n\n\n\n\n\n\n\n\n\n")
    else:
        first(letter1, letter2, letter3)

letter1=0
letter2=0
letter3=0
first(letter1, letter2, letter3)

Now 16384 is the depth only with the second function. Adding in the third makes it (128*128*128 or 2097152) which is way too large for recursion. So my recommendation for solving your problem is to not use recursion. Try nested for loops instead. For example:

for letter3 in range(128):
    for letter2 in range(128):
        for letter1 in range(128):
            print(chr(letter1), chr(letter2), chr(letter3))
print("completed")

Note that this will still take a while since it is making 128^3 print statements.

Community
  • 1
  • 1
David C
  • 1,898
  • 2
  • 16
  • 31
1

Each time you call a function a frame gets added to the call stack, and there is a limit to the number of frames on the call stack. When that limit is exceeded you get recursion depth exceeded error.

In your code there are 127 first() frames put on the stack, then 127**2 first() and second() pairs of frames put on the stack, and then 127**3 first(), second(), and third() triplets of frames put on the stack before the stack unrolls. That gives a total of 2064639 frames.

Lance Helsten
  • 9,457
  • 3
  • 16
  • 16