# Loop reached Maximum Recursion depth

**URL:** <https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684>\
**Category:** Python Help\
**Created:** [June 20, 2022, 3:28pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684 "2022-06-20T15:28:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![darkveider](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@darkveider](https://discuss.python.org/u/darkveider)\
**Post date:** [June 20, 2022, 3:28pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/1 "2022-06-20T15:28:19Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 20, 2022, 3:42pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/2 "2022-06-20T15:42:29Z")

</div>

> [@darkveider](#):
>
> So if temp is less then 20 it should repeat function.

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?

---

<div class="post-metadata">

**Author:** ![darkveider](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@darkveider](https://discuss.python.org/u/darkveider)\
**Post date:** [June 20, 2022, 3:46pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/3 "2022-06-20T15:46:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 20, 2022, 3:51pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/4 "2022-06-20T15:51:56Z")

</div>

Not too sure if this is what you want:

```python
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:

```python
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

```

---

<div class="post-metadata">

**Author:** ![darkveider](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@darkveider](https://discuss.python.org/u/darkveider)\
**Post date:** [June 20, 2022, 4:05pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/5 "2022-06-20T16:05:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 20, 2022, 4:06pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/6 "2022-06-20T16:06:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![darkveider](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@darkveider](https://discuss.python.org/u/darkveider)\
**Post date:** [June 20, 2022, 4:08pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/7 "2022-06-20T16:08:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [June 20, 2022, 4:14pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/8 "2022-06-20T16:14:11Z")

</div>

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:

````auto
```python
your code
```

````

> **[Markdown Guide](https://www.markdownguide.org/)**
>
> A free and open-source reference guide that explains how to use Markdown.

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

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [June 20, 2022, 5:25pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/9 "2022-06-20T17:25:41Z")

</div>

> [@darkveider](#):
>
> So this while syntax should avoid this error?

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?

```python
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:

```python
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):

```python
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

```

---

<div class="post-metadata">

**Author:** ![darkveider](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@darkveider](https://discuss.python.org/u/darkveider)\
**Post date:** [June 22, 2022, 4:47pm UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/10 "2022-06-22T16:47:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ullix](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ullix/32/4962_2.png) [@ullix](https://discuss.python.org/u/ullix)\
**Post date:** [June 23, 2022, 7:46am UTC](https://discuss.python.org/t/loop-reached-maximum-recursion-depth/16684/11 "2022-06-23T07:46:07Z")

</div>

> 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.
