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