-2

I have this code:

def get_girlfriend():
  res = input("Will you go out with me? ")
  if (res == "y"):
    print("We've done it bois")
    return
  get_girlfriend()

but I keep getting this error:

RecursionError: maximum recursion depth exceeded

Could anyone help?

  • 1
    The call to `get_girlfriend()` is indented inside the function, which you probably didn't mean to do. – John Gordon Nov 11 '21 at 04:06
  • 1
    No I'm calling it inside the function itself – your_average_programmer Nov 11 '21 at 04:06
  • 1
    The function calls itself. Which calls itself. Which calls itself. And so on, endlessly. (Assuming the answer is always no) The critical part is that when the function calls itself, _the original execution isn't done yet_ (even though that's the last line of the function). So you end up with hundreds of copies of the function stacked up on top of each other. – John Gordon Nov 11 '21 at 04:12
  • 3
    I can't reproduce the error. I'm able to input `n` a few times, then `y`. Are you inputting something other than `y` over 1000 times? – luther Nov 11 '21 at 04:13
  • disclaimer: this is bad, don't do it, it's not funny. Anyhow, are you actually hitting return enough to hit the recursion limit? Obviously an infinite recursion will crash eventually in any language (see the name of this website, literally), but sooner in python because it intentionally throws an exception to stop you. – Kenny Ostrom Nov 11 '21 at 04:13
  • @KennyOstrom In a language that optimized tail calls (sometimes called tail call elimination), this code would not ever blow the stack but instead would simply be an infinite loop (assuming "y" was never entered). – David Conrad Nov 11 '21 at 04:17
  • It occurs to me that this whole question might be a joke on incels, considering what generates this error. – luther Nov 11 '21 at 04:51
  • @DavidConrad correct, I messed myself up again making generalizations – Kenny Ostrom Nov 11 '21 at 13:34

3 Answers3

0

The default maximum recursion depth for python is 1000.

So when the function calls itself to 1000 times, you get the error. I do not know what you aim to achieve by your code but by adding some condition if the statement satisfies your condition, you will be able to terminate.

collinsmarra
  • 148
  • 1
  • 7
0

Default recursion depth in python is 1000, but if you need to increase the depth (Not recommended) by;

import sys
sys.setrecursionlimit(needed_depth)

But if you're trying to run your simple program without any Error, try to capture them and handle or set a base limit for the recursion.

Set a base limit for recursion:

def get_girlfriend(n=0):
    res = input("Will you go out with me? ")
    if (n < 100 and res == "y"):
        print("We've done it bois")
        return
    get_girlfriend(n+1)

Try and Capture the Error:

def get_girlfriend():
    res = input("Will you go out with me? ")
    if (res == "y"):
        print("We've done it bois")
        return
    try:
        get_girlfriend()
    except RecursionError:
        return

A Function should always have an exit gateway.

  • `sys.setrecursionlimit(needed_depth)` should never be used. It just kicks the can a bit down the road at best, and brings down the whole interpreter process at worst. Simply don't use recursion for collecting user input. Use a loop. – ggorlen Nov 11 '21 at 17:05
  • That is why I said that I don't recommend the way to change the recursion depth. Python sets the default limit so that any program wouldn't go into stack overflow regardless of the stack allocated in the hardware. If the hardware has the capacity to avoid the stack overflow over a limit then changing the recursion depth wouldn't cause any problem. Since Python programs are platform-independent changing the recursion depth can cause an error in different hardware configured computers. I believe the question isn't too literal, but to demonstrate the Recursion depth error. – Kabilan Mahathevan Nov 11 '21 at 17:54
  • "Since Python programs are platform-independent changing the recursion depth can cause an error in different hardware configured computers." exactly. When you mess with the depth, you really never know when you'll kill the interpreter, even if it appears to work on your system. – ggorlen Nov 11 '21 at 17:59
-1
def get_girlfriend():
    res = input("Will you go out with me? ")
    answer = res
    if (answer == "y"):
        print("We've done it bois")

get_girlfriend()

idk if this what you mean. But here's the output if you type y

Will you go out with me? y We've done it bois

and others but y: Will you go out with me? n

zzzz
  • 71
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 11 '21 at 05:12