1

The structure of my code is as follows:

def collect_data(text):
    for line in text:
        key, match = parse_lines(line) #here I am parsing using a dict and regular expressions

        if key == something_1:
            var1 = match.group(1)
            list1.append(var1)

        elif key == something_2:
            var2 = match.group(2)
            list2.append(var2)

        elif key == something_3:
            func()

def func():
    for element in elements :
        if element == element1:
            variation = float(var1)
        elif element == element2:
            variation = float(var2)

         #here I append the variable variation to a list depending on the element name e.g. somelist.append(variation)
collect_data(text)

I am trying to parse through lines similar to the solution given in here link. In two first conditionals I append either var1 or var2 to some lists and it works as it should. The problem is with func(). I am getting: NameError: name 'var1' is not defined.

In the text that I am parsing, the first two conditions in collect_data(text) typically occur before the third one but I don't think this is the nature of my problem. I just don't know how to make Python see that var1 and var2 are (will be) defined once the conditions are met. I hope the explanation is clear enough. Perhaps using a class would be a solution? Does anyone have any suggestions?

3 Answers3

0

Try passing the var1 and var2 arguments to func(). Like this:

def func(var1, var2):
    # your code
TaxpayersMoney
  • 669
  • 1
  • 8
  • 26
0

You have two problems:

  1. var1 and var 2 are local variables, so you can only access them in your collect_data function, you can't access them in func(). You can use global variables, or you have to pass them as arguments (best solution in your case),
  2. You have to create var 1 and var2 before your if statements if you want them to be accessible in the last elif:

Try something like this:

def collect_data(text):
    for line in text:
        key, match = parse_lines(line) #here I am parsing using a dict and regular expressions

        var1 = match.group(1)
        var2 = match.group(2)

        if key == something_1:
            list1.append(var1)

        elif key == something_2:
            list2.append(var2)

        elif key == something_3:
            func(var1, var2)

def func(var1, var2):
    for element in elements :
        if element == element1:
            variation = float(var1)
        elif element == element2:
            variation = float(var2)

         #here I append the variable variation to a list depending on the element name e.g. somelist.append(variation)
collect_data(text)
Axel Puig
  • 1,304
  • 9
  • 19
0

In Python, a function basically only knows the variables that are specified within the function or in the main code. It is unaware of anything that happens in other functions unless you parse them into each other.

def collect_data(text):
    for line in text:
        key, match = parse_lines(line) #here I am parsing using a dict and regular expressions

        if key == something_1:
            var1 = match.group(1)
            list1.append(var1)

        elif key == something_2:
            var2 = match.group(2)
            list2.append(var2)

        elif key == something_3:
            list3.append(func(a=var1, b=var2))

def func(a, b):
    for element in elements :
        if element == element1:
            variation = float(a)
        elif element == element2:
            variation = float(b)
    return variation


# here I append the variable variation to a list depending on the element name e.g. somelist.append(variation)
collect_data(text)
offeltoffel
  • 2,691
  • 2
  • 21
  • 35