-2

I am getting a variable assignment error and am obviously not understanding something but from my code I don't see why it is happening.

The code:

   def something(filename):
    with open("tmp/"+filename.stem+".txt", "r") as infile: # opening the tmp file for scraping the data
        readfile = infile.readlines() #reads the infile lne by line and returns a list containing the lines
        for i, line in enumerate(readfile[1:], 1): # looping over all the lines in the file from position 1 (so skipping 0) to avoid circular feedback
            if 'Overview' in line:
                start = i
                continue
                for i, line in enumerate(readfile[1:], 1):
                    if 'Identified secondary metabolite regions using strictness' in line:
                        end = i
                        marker = list(map(lambda s: s.strip('\n'), readfile[start + 1:end])) # stripping the '\n' off every element in the list. map executes a function for each element in a sequence
        for i, line in enumerate(readfile[1:], 1): # looping over all the lines in the file from position 1 (so skipping 0) to avoid circular feedback
            for location in marker:

The error:

UnboundLocalError: local variable 'marker' referenced before assignment

Should markernot always be assigned within the scope of the function due to the first forloop executing before the second one resulting in markerbeing set?

secondly this seems messy to use two for loops, how can I do this within one for loop?

Lamma
  • 895
  • 1
  • 12
  • 26
  • 1
    `marker` is only defined if `readfile[1:]` isn't empty, `if 'Overview' in line`, and `if 'Identified secondary metabolite regions using strictness' in line`. Apparently one of those conditions doesn't hold true, so the `marker = ...` line is never reached! – deceze Mar 02 '20 at 12:18
  • If the if condition "if 'Identified secondary metabolite regions using strictness' in line" is not satisfied it simply ignores the marker declaration. At the begging of your code put a marker = []. Also it would be useful to give a file example, otherwise the community cannot try your code. – TeArge Mar 02 '20 at 12:19
  • Also `marker` is assigned to a new list at each iteration instead of appending things. This means that when you do `for location in marker:` in the last line it will only take the marker from the vary last iteration of the previous nested loop. Even if you fix your error, your code might not be doing what you think it does. –  Mar 02 '20 at 12:24
  • @TeArge But at some point in the for loop iteration `marker`will be assigned and as both for loops are in the same scope that should carry over to the second loop? – Lamma Mar 02 '20 at 12:25
  • @SembeiNorimaki the conditions to asign marker should only ever be met once. – Lamma Mar 02 '20 at 12:26
  • 1
    Also inside `if 'Overview' in line:` you have a `continue`. Which means that `marker` will never be assigned to anything. You have a block of code that is unreachable –  Mar 02 '20 at 12:28
  • ah yes that is it! I meant it to be if not continue. Tnak you! I still want to find a way to avoid using two for loops though. Any suggestions? – Lamma Mar 02 '20 at 12:30

3 Answers3

2

The point is that marker is assigned in an "if" condition. What about if it's false ?

jpaul
  • 309
  • 1
  • 5
  • But at some point in the for loop iteration it will be asigned and as both the for loops are within the scope of the same function, should `marker` not carry over into thr second for loop as being assigned? – Lamma Mar 02 '20 at 12:19
  • You're getting this error because `marker` is not assigned. – Siddharth Prajosh Mar 02 '20 at 12:50
1

marker is no set when the condition if 'Identified secondary metabolite regions using strictness' in line: is not satisfied. Set marker = [] at the begining.

miszcz2137
  • 894
  • 6
  • 18
  • But at some point in the for loop iteration it will be asigned and as both the for loops are within the scope of the same function, should `marker` not carry over into the second for loop as being assigned? – Lamma Mar 02 '20 at 12:20
1

Try defining marker variable outside of the function. Then use it in the funtion.

refer -> Don't understand why UnboundLocalError occurs