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.