I have the following code:
import fileinput
import sys
import re
def reading_fasta(fasta):
#### defining variables
name = ""
hashseq = {}
sequence = ""
### loop over file
for line in fileinput.input(fasta):
if not line:
hashseq[name] = sequence
fileinput.close()
break
elif re.match("^>.*", line):
if fileinput.lineno() != 1:
hashseq[name] = sequence
del sequence
name = re.split('\W+', line)[1]
else:
line.rstrip("\n")
sequence += line
reading_fasta(sys.argv[1])
with the following error:
Traceback (most recent call last):
File "parse.py", line 25, in <module>
reading_fasta(sys.argv[1])
File "parse.py", line 23, in reading_fasta
sequence += line
UnboundLocalError: local variable 'sequence' referenced before assignment
Why is that? Surfing on the net I've found same errors but because of variables were set at the global environment. Nevertheless, my variables are defined inside the function, as local variables.