0

I am in an introductory Python class which is my first exposure to programming. Any help much appreciated. I am creating a piece of code that is to determine a linear regression function for a given set of inputs from a text file. I have defined the variable which contains the list of ordered pairs (x,y), as global pairs. However, I keep getting the error that pairs is not defined. I can't tweak any other parts of my code because this list is empty, causing the other lists I derived from this one to also be empty. I am genuinely stuck on this, I have looked for the answer on this site and others but I have not yet found the solution.

This is some of the code I have:

#read values into tuple to seperate the spaces from X and Y values from the text file
#convert the tuple to a list containing (x,y) paris
    #the values are stored if we call the funciton
    #but the list of pairs doesn't seem to be global, it is empty when i just print(pairs)
def list_comprehension(in2):

    infile = open("in2",'r')
    global coordinates
    coordinates = (line.split() for line in infile)
    infile.close()
    global pairs
    pairs = [(float(x),float(y)) for x,y in coordinates]
    pairs.append(coordinates)

    return pairs 

#isolate x and y variables into seperate lists
    #same problem, the funciton operates fine
    #but the lists have nothing in them because pairs has nothing in it
X=[]
Y=[]
def isolate(X,Y):
    for (x,y) in pairs:
        X.append(x)
        Y.append(y)

    return X, Y

And the error is this:

 File "C:/Python34/python/Program 5/p5 draft function and values.py", line 47, in isolate
    for (x,y) in pairs:
NameError: name 'pairs' is not defined
Shapi
  • 5,493
  • 4
  • 28
  • 39
mmb_rach
  • 163
  • 1
  • 1
  • 12
  • 1
    http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python – R Nar Oct 21 '15 at 22:34
  • 1
    You don't call list_comprehension() in the code posted so pairs is never created. When you do call it, remember to catch the return, --> pairs= list_comprehension(in2) –  Oct 21 '15 at 22:35
  • You don't have a `global pairs` declaration in `isolate`, only in `list_comprehension`. – Barmar Oct 21 '15 at 23:15

3 Answers3

1

My guess is that isolate is called at some point in time before list_comprehension, meaning that the global name pairs has not yet been defined.

>>> def init():
...    global z
...    z = 2
... 
>>> z
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'z' is not defined
>>> init()
>>> z
2

PRECAUTIONARY NOTE

It isn't recommended to use the global keyword because it makes it difficult to track where all the global variables are. Instead, I recommend declaring pairs in your main function and then set pairs = list_comprehension(...) and passing it into isolate(X,Y,pairs). See Use of "global" keyword in Python as mentioned by @r-nar in the comments.

Community
  • 1
  • 1
Almog
  • 731
  • 4
  • 10
  • Thank you, I am going to define it in the main and do as you said and I think it will work. And I realize the problem, the problem is I never actually call the function list_comprehension in my print function. Pairs never needs to be printed, it only is used for the remainder of the program. Due to this I think your method will be the right way to go about it – mmb_rach Oct 21 '15 at 22:53
  • When I set pairs = list_comprehension(...), we can print pairs in the main function and we get the values needed. However, it also tacks onto the end, [(0.28, 6.62), (0.5, 5.93), (0.67, 4.46), (0.93, 4.25), (1.15, 3.3), (1.38, 3.15), (1.6, 2.43), (1.98, 1.46), at 0x02F9C800>]. I am not sure what this is, if i use list_comprhension(in2) it says in2 not defined – mmb_rach Oct 21 '15 at 23:14
  • by (...) i mean to pass in whatever values of in2 you inteded to use. it seems that you've hardcoded the value of in2 to be "in2" so you can completely remove the argument in2 from the method signature. As far as a generator object goes - you should see https://wiki.python.org/moin/Generators which explains why is tacked on at the end (in short its a reference to the generator object) – Almog Oct 21 '15 at 23:18
  • Also you should not have `.append(coordinates)` as it is already being done in your list comprehension the line above. That's why you're getting the generator object. – Almog Oct 21 '15 at 23:21
0

The global keyword is used when you are declaring global scope for a variable within a function. It is required when you wish to modify a global variable. Check to make sure you have declared pairs outside of the function.

https://docs.python.org/2/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

McGlothlin
  • 2,059
  • 1
  • 15
  • 28
0

The error message says the problem is in pairs. The global declaration in list_comprehension has no effect on pairs, it needs its own global declaration.

def isolate(X,Y):
    global pairs
    for (x,y) in pairs:
        X.append(x)
        Y.append(y)

    return X, Y
Barmar
  • 741,623
  • 53
  • 500
  • 612