ARTICLE AD BOX
I’m experimenting with recursion and noticed a confusing behavior.
When I call a recursive function normally, it works as expected.
But when I call the same function inside a loop, the recursion depth seems to increase faster than expected, eventually hitting RecursionError even for smaller inputs.
New contributor
harshit bajpai is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4

"why is my python" with a javascript tag - tags are important for visibility, make sure you have the right one. It is not clear from your description what you might be seeing, please add a minimal reproducible example to show your problem in action.
2025-11-27 09:25:44 +00:00
Commented 9 hours ago
Please read How to Ask, and take some proper care with your question. If this really is a python code issue then a) why did you tag JavasScript? They're not even similar, so it can't be just a typo or something. And it means your question will just be seen by the wrong people, who can't help. And b) why haven't you shared your code? How do you think we can comment on what is happening if we can't see it? Please remember we can't see your screen or read your mind. If you're serious about getting assistance then please edit your post. We'll be happy to help you if your question makes sense.
2025-11-27 09:33:40 +00:00
Commented 9 hours ago
the recursion depth seems to increase faster than expected...ok, so what did you expect, and why? What exactly did you observe instead? Provide some meaningful statistics along with your code. Vague assertions are no use to anybody - as a programmer you hopefully will understand this. Details and specifics are very important.
2025-11-27 09:34:40 +00:00
Commented 9 hours ago
why because there's a bug in the code. The question contains no code so we can't guess what's going on. Although, if you call a function recursively in a loop but check the termination condition after the loop, none of the calls will ever reach it. All will keep spawning new calls forever. That's a code bug
2025-11-27 10:16:25 +00:00
Commented 8 hours ago