Loop reached Maximum Recursion depth

Hi,
I write simple script to get temp from other source. But after some hours it get error with maximum recursion depth exceeded. So if temp is less then 20 it should repeat function. How I can solved it?
def main()
a=temp(get temp from other source)
if a>=20
#do something
else:
main()

This is a little unclear, but what I think you mean is, while a < 20: #repeat function

In other words, keep getting a value from temp, assign that to a, keep doing that until a is 20 or greater, at which point continue with the rest of the script, yes?

Yes, but if I use:
def main()
a=temp(get temp from other source)
while a>=20
#do something
else:
main()

then with while cycle read temp only one time?

Not too sure if this is what you want:

a = 0 # initialise a

while a <= 20:
    a=temp(get temp from other source) # this will not stop until a > 20

# the rest of the scriot goes here

… or maybe:

def get_data():
    a = 0
    while a <= 20:
       a=temp(get temp from other source)
    return a

get_data() # this will not stop until a > 20

So this while syntax should avoid this error? “error-maximum-recursion-depth-exceeded-while-calling-a-python-object”
I will try

It depends on the external data: what if a is never greater than 20? You want the script to run forever??

on theoretical calculation it should not be longer than 4-5 days.

I’ve not tried tried anything like this, but in theory, I see no reason why it should stop until a is 21 or greater.

There are Forum members here that have a deeper understanding of Python than me, so maybe wait a while and see if you get a better answer than the one I’ve posted.

As an aside: when posting code, you should make it as easy as possible for others to help you, so to that end, you should use what is called ‘Markdown’ to format your code, like this:

```python
your code
```

There’s also to button </> at the top of the input box; either should work.

We are just guessing how the code looks like because we do not have real runnable code and the code you posted here lost its indentation (it is not between triple backticks), is missing some parts (like :) etc. It is always best to prepare a minimal runnable example code which anyone can run.

Did you want to post something like this?

def main():
    a = temp()      # get temp from other source
    if a >= 20:
        pass        # do something here
    else:
        main()

This code allows endless recursion (a function calls itself). Every function call needs to allocate additional memory so the number of unfinished function calls is limited. This is the reason you receive the error message “maximum recursion depth exceeded” when you exceed the limit.

You get the same functionality without the call overhead and possible error if you use a loop instead of recursion. Here is an equivalent of the code above without using the recursion:

def main():
    while True:
        a = temp()      # get temp from other source
        if a >= 20:
            break
    pass                # do something here

In fact normally it is better to avoid recursion if you can solve the problem in a similar complexity without it.

Also the code Rob posted is equivalent (with just a small fix of the condition negation):

def main():
    a = 0               # initial value which does not break the condition
    while a < 20:
        a = temp()      # get temp from other source
    pass                # do something here
1 Like

It looks that while cycle solved the issue. Thanks for help.

def main()
a=temp(get temp from other source)
if a>=20
#do something
else:
main()

it looks to me like you are calling main() from within main()?

Don’t do this! It will certainly result in reaching the maximum recursion limit and that limit will likely change in a modified program.