python - Prevent Exceeding Maximum Recursion Depth -


i'm trying make small script , efficient way of doing involves calling function within itself. however, gives message "runtimeerror: maximum recursion depth exceeded while getting str of object".

i tried cause program exit before reached this, not seem job. i'm wondering if there way stop program after number of runs error not happen. here tried fix this:

import sys n = 0 def cycle(b,n):     total = 0     in str(n):         y in i:          total+=int(y)**b     n+=1     print(total)     if n == 10:      sys.exit()     else:      cycle(b,total) cycle(2,562) 

thanks.

try passing in counter , avoid giving confusing variable names:

import sys def cycle(b,n, counter):     total = 0     in str(n):         y in i:          total+=int(y)**b     counter+=1     print(total)     if counter == 10:      sys.exit()     else:      cycle(b,total,counter) cycle(2,562,0) 

run here.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -