I'm currently having a logic issue with a recursive statement. My code is as follows
def find_postpep_site(string):
if(re.search('(G[RK])|(GKR)|(G$)', string)):
lastindex = (re.search('(G[RK])|(GKR)|(G$)', string)).end()
find_postpep_site(string[lastindex:10000])
else:
return lastindex
With the output as follows
Traceback (most recent call last):
File "cleavage_test.py", line 47, in <module>
mature = (data[find_pp_site(data):find_postpep_site(data)])
File "cleavage_test.py", line 38, in find_postpep_site
find_postpep_site(string[lastindex:10000])
File "cleavage_test.py", line 38, in find_postpep_site
find_postpep_site(string[lastindex:10000])
File "cleavage_test.py", line 41, in find_postpep_site
return(lastindex)
UnboundLocalError: local variable 'lastindex' referenced before assignment
This issue isn't incredible complicated, however it is frustrating. I want to have this method self contained and do not want to initialize and assign a variable in the main body of the program.
So my question is thus, how can I assign lastindex a value without resetting it every time the method runs recursively? For example (bad example)
def find_postpep_site(string):
lastindex = 0
if(re.search('(G[RK])|(GKR)|(G$)', string)):
lastindex = (re.search('(G[RK])|(GKR)|(G$)', string)).end()
find_postpep_site(string[lastindex:10000])
else:
return lastindex
Which will always return zero
Thanks