Python Forum
maximum recursion depth exceeded while calling a Python object error in python3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
maximum recursion depth exceeded while calling a Python object error in python3
#1
Bug 
import time
import random
import string

#Searching vars
tries = 0
found = False

def website():
    print("Website: " + str(res) + "\n")
    print("Q - Exit")
    print("W - Add to favorites")
    print("E - Find another page")
    usr_input = input("> ")
    if usr_input == q:
        exit()
    elif usr_input == w:
        print("Added to favorites\n")
        website()
    elif usr_input == e:
        searching()
    else:
        print("Error, There is no such option.")
        website()

def stats():
    global results
    global tries
    global found
    timer = time.time()
    print("Tries:" + str(tries))
    print("It took:" + str(timer))
    print("\nTo view page press enter.")
    usr_input = input("> ")

def searching():
    global tries

    # Adding and subtracting letters
    tries += 1
    chars = 50
    add_num = False
    while add_num == True:
        chars += 1
        if chars == 50:
            add_num = False
            searching()
        print(chars)
    while chars == False:
        chars -= 1
        if chars == 12:
            add_num = True
            searching()
        print(chars)
    if found == True:
        stats()

    #start
    res = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k = chars))
    res += ".onion"
    ######
    searching()

print("Press enter to find random dark-net website")
usr_input = input("> ")
print("Searching for website...")
time.sleep(10)
searching()
My code suppose to find random dark-net websites by generating random strings with numbers and letters.

error:maximum recursion depth exceeded while calling a Python object

I know what this error means but I don't know how to fix it.

Thanks in advance!
Reply
#2
I don't see that this is a problem where recursion is applicable. At least not the way you are doing it. Recursion is useful when future results are dependent on prior results. Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2). Where is your recursion? Each time you search you start the search over.

To me it looks like you are trying to write a function that generates random strings between 12 and 50 characters long. I think you are planning to test these strings to see if they are are a web page address (currently no test). But this is not what your program does. Your program generates random strings that are all 50 characters long.
def searching():
    global tries
 
    # Adding and subtracting letters
    tries += 1
    chars = 50      # Always starting with chars = 50 whenever called
    add_num = False # Always initialized to false
    while add_num == True:  # add_num is always False.  This is never called
        chars += 1
        if chars == 50:
            add_num = False
            searching()
        print(chars)
    while chars == False:  # chars is always 50, so this never executes either
        chars -= 1
        if chars == 12:
            add_num = True
            searching()
        print(chars)
    if found == True:   # found is always false, so this never gets called
        stats()

    # This is the only code that runs.  chars is always 50.
    #start
    res = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k = chars))
    res += ".onion"
    ######
    searching()
I think a simple loop will do what you want to do. I do not understand the logic of incrementing and decrementing the number of chars, so I will use random to select the string length.
while True:
    chars = random.randrange(20, 50)
    res = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k = chars))
    res += ".onion"
    # do some test to verify this is a website.
    if some_test_function(res):
        print("Website:", res)
        print("Q - Exit")
        print("W - Add to favorites")
        print("E - Find another page")
        usr_input = input("> ")
        if usr_input == 'q':
            break
        elif usr_input == 'w':
            # Add website to favorites
Reply
#3
Never use if var == True: - use if var:

Likewise, use if var == False: - use if not var:

Booleans can have only 2 values - True and False. var == True creates .... another boolean - which will be True if var is True and False - if var is False

Source - PEP-8. PEP-8 recommends using truthiness even of non-boolean variables directly, e.g. if list_: instead of if len(list_):
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
(Aug-01-2020, 10:44 PM)deanhystad Wrote: To me, it looks like you are trying to write a function that generates random strings between 12 and 50 characters long. I think you are planning to test these strings to see if they are are a web page address (currently no test). But this is not what your program does. Your program generates random strings that are all 50 characters long.

I'll add it later.

Now I'm trying to create random strings. But I have a bug:
Quote:"maximum recursion depth exceeded while calling a Python object"
Reply
#5
Recursion is when a function calls itself. Your functions should not call themselves. searching should not call searching() and website should not call website(). Recursion can be used to solve lots of interesting problems, but yours is not one of them. You should use loops.

Recursion is causing your program to crash because Python puts a limit on how "deep" the recursion can go. Each recursive function call uses a bunch of memory. The memory is used to save information about what the function was doing (program counter, variable values, etc) so this can be restored when the function call returns. Left unchecked your program would use all available memory and crash, and probably cause other programs to crash. Your searching() function makes one string and then calls itself, making another string and then calling itself to make another.....

Instead of recursively calling itself your searching function should generate a string, test if that is a website, add the website if the test passes, and repeat over and over. Written this way, with a loop, it could run forever because it does not continuously consume more and more memory saving context information for yet another function call.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python script is hanging while calling a procedure in database prasanthi417 4 442 Jan-17-2024, 02:33 PM
Last Post: deanhystad
  Pyinstaller Maximum recursion bug scales11 8 10,892 Nov-10-2023, 10:26 PM
Last Post: SuzanneKH09
  Need help with 'str' object is not callable error. Fare 4 775 Jul-23-2023, 02:25 PM
Last Post: Fare
  pyscript index error while calling input from html form pyscript_dude 2 938 May-21-2023, 08:17 AM
Last Post: snippsat
  Understanding and debugging memory error crashes with python3.10.10 Arkaik 5 1,977 Apr-18-2023, 03:22 AM
Last Post: Larz60+
  PyRun_SimpleFile calling multiprocessing Python Class cause endless init loop Xeno 2 988 Sep-19-2022, 02:32 AM
Last Post: Xeno
  Error in Int object is not subscript-able. kakut 2 1,131 Jul-06-2022, 08:31 AM
Last Post: ibreeden
  python update binary object (override delivered Object properties) pierre38 4 1,710 May-19-2022, 07:52 AM
Last Post: pierre38
  Max recursion depth.... Error MeloB 2 1,837 Feb-16-2022, 05:21 PM
Last Post: MeloB
  Calling python from c++ in visual studio pdk5 0 2,128 May-24-2021, 10:18 AM
Last Post: pdk5

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020