Skip to content
Advertisement

how to handle big loops in python?

This is more to do with my code as i’m sure i’m doing something wrong

all i’m trying to do is call a main function to repeated itself with a recursive number that at some point i’ll reset.

here is my code

JavaScript

At first the main thread runs fine, but somewhere in the 2500 interval +- it just stops with the error code for stack overflow

JavaScript

I am using Windows and pyCharm

do you know how I can handle that ? or am i doing something wrong ?

Advertisement

Answer

You are most likely recursively looping forever. You recursively call mainThread in these parts:

JavaScript

But you never return to the caller. You need a base case that stops the recursive loop


Consider this example of a recursive factorial function:

JavaScript

The base case (terminating case) is when x <= 1. and this will be reached eventually as fac(x-1) implies that x is decreasing towards the conditional x <= 1. i.e. you need to have some sort of case where recursion is not needed. Your current function is recursively equivalent to:

JavaScript

Perhaps infinitely looping and locally updating variables will work?

JavaScript
Advertisement