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
  Calling python from c++ in visual studio pdk5 0 587 May-24-2021, 10:18 AM
Last Post: pdk5
Star Type Error: 'in' object is not callable nman52 3 763 May-01-2021, 11:03 PM
Last Post: nman52
  Time Limit Exceeded error loves 5 847 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
  Calling Oracle REST SQL from Python johnjacob 2 667 Nov-05-2020, 04:19 AM
Last Post: johnjacob
  Error in Int object is not subscript-able. How to debug this ? yanDvator 1 947 Aug-03-2020, 02:28 PM
Last Post: Larz60+
  how to solve "a bytes-like object is required, not 'str'" error maiya 2 1,028 Jul-28-2020, 07:03 PM
Last Post: bowlofred
  import yaml error for python3 maiya 4 6,181 Jul-15-2020, 06:07 PM
Last Post: Gribouillis
  Getting error "TypeError: 'int' object is not callable" while using ylim maxprime_bhisham 2 1,858 Apr-26-2020, 10:55 AM
Last Post: DataAnalyticsIreland
  why this error occured in recursion ashishraikwar 1 788 Apr-24-2020, 11:12 AM
Last Post: buran
  Lambda function recursion error DeadlySocks 1 814 Apr-13-2020, 05:09 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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