I have created a dictionary and I am trying to find if the elements in a list called "seqrecord" are the keys of the dictionary. The script I have done is below:
def read_rebase(Renzymes): renzymedict = {} filenz = open(Renzymes) for line in filenz.xreadlines(): fields = line.split() name = fields[1] pattern = fields[2] renzymedict[(pattern)] = name filenz.close() return renzymedict ren = {} ren = read_rebase(Renzymes) for k in seqrecord: if k in ren: print k, ren[k]
It doesn't work...
NameError: name 'ren' is not defined
What I am doing wrong? 'ren' should be the dictionary...
Thank you
after a
return
statement nothing will be executed! the function returns to its call by returningrenzymedict
I have deleted return statement, but the result is the same (
'ren' is not defined
)Maybe your indentation is wrong in your posted code?
try this
I don't think so,
are "inside" the
def read_base
function...but this will not work. if you put it outside of the function it will work. it works like this:
now ren is declared and a known variable which can be used in
Ok, modified this error, now there is a new one...
(Thank you for your answers)
Renzymes is variable which should contain your filename.
Sorry, I don't understand what I should do
Here is how you defined your function
read_rebase
:When you call this function, you need to provide some value for the local variable
Renzymes
that is a path to a file somewhere. For example:The file
/your/path/and/file.txt
will get opened in the functionread_rebase
.If you instead call this function like so:
Then you need to have defined a global variable (also poorly and ambiguously named as
Renzymes
) somewhere further up in the code. If this variable is not defined, then theread_rebase
function cannot work.So either set the path to the file explicitly, as shown above, or set a sensibly-named variable and pass that to your function, e.g.:
Thank you so much! It works! :)