Я учусь на вводном Python
курсе, который является моим первым знакомством с программированием. Любая помощь высоко ценится. Я создаю фрагмент кода, который должен определить функцию линейной регрессии для заданного набора входных данных из текстового файла. Я определил переменную, которая содержит список упорядоченных пар (x,y)
, как глобальные пары. Однако я продолжаю получать сообщение об ошибке, что пары не определены. Я не могу настроить какие-либо другие части моего кода, потому что этот список пуст, в результате чего другие списки, которые я получил из этого, также будут пустыми. Я действительно застрял в этом, я искал ответ на этом сайте и других, но я еще не нашел решения.
Это часть кода, который у меня есть:
#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
А ошибка вот в чем:
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
global pairs
вisolate
, только вlist_comprehension
. - person Barmar   schedule 22.10.2015