Python 我得到了';名称错误:全局名称';y';未定义';编译时出错。如何修复此错误? 随机导入 进口鼠尾草 进口numpy def flip(): 如果随机均匀(0,1)30打印(a();“”“).strip() 在() 在a()

Python 我得到了';名称错误:全局名称';y';未定义';编译时出错。如何修复此错误? 随机导入 进口鼠尾草 进口numpy def flip(): 如果随机均匀(0,1)30打印(a();“”“).strip() 在() 在a(),python,nameerror,Python,Nameerror,我得到了';名称错误:全局名称';y';未定义';编译时出错。如何修复此错误? 随机导入 进口鼠尾草 进口numpy def flip(): 如果随机均匀(0,1)30打印(a();“”“).strip() 在() 在a()中 NameError:未定义全局名称“y” Glib回答:定义它。但说真的,你在return语句中使用它:你希望它在哪里收到值?我想你可能想要p.x和p.y——或者不想要。我想知道p是否是point的缩写,但它显然是很多东西。也许你想要更好的

我得到了';名称错误:全局名称';y';未定义';编译时出错。如何修复此错误?
随机导入
进口鼠尾草
进口numpy
def flip():
如果随机均匀(0,1)<0.5:
返回0
返回1
定义a():
[6,0 0 0 0,0 0 0 0,0 0 0 0,0 0 0,0 0 0 0,0 0 0,0 0 0,0 0 0,1,5,0 0,1,0 0,1,1,1,0 0 0,1,0 0 0,0,0 0 0,0 0 0,0 0,0 0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,1,1,0,1,1,1,0,1,1,1,0 0,1,1,1,0,1,1,1,1,1,0,0 0,1,00 0,1,1,1,1,1,1,1,1,00 0,1,1,1,1,1,1,1,1,00 0,1,1,1,1,5 5 5 5 5 5 5 5 5 5 5,1,1,1,1,1,1 1 1],[0,1,0],[1,0,0],[0,0,2],[2,0,0],[0,0,3,0],[3,0,0],[0,0,4],[0,4,0],[4,0,0],[0,0,5,0],[5,0,0],[1,0,4],[1,0,3],[1,0,2],[1,0,1],[1,4,0],[1,3,0],[1,2,0],[1,1,0],[2,0,3],[2,0,2],[2,0,1],[2,3,0],[2,2,0],[2,1,0],[3,2,0],[3,1,0],[3,0,1],[3,0,2],[4,1,0],[4,0,1]];
印刷品(len(L));
L6=[m表示L中的m,如果和(m)==6];
打印(len(L6));
Lless=[m表示L中的m,如果m不在L6];
印刷品(无透镜);
S=[];
对于范围内的i(len(L6)):
如果翻转():
S.append(L6[i])
如果S=[]:
S=[L6[0]]
对于范围内的i(len(less)):
如果翻转():
S.append(无[i])
P=[[ZZ.random_element(),m]表示S中的m]
返回和([p[0]*x**p[1][0]*y**p[1][1]*z**p[1][2]表示p中的p])
打印(a());
61
25
36
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
28返回值([p[0]*x**p[1][0]*(y**p[1][1])*(z**p[1][2])表示p中的p])
29
--->30打印(a();“”“).strip()
在()
在a()中
NameError:未定义全局名称“y”

Glib回答:定义它。但说真的,你在
return
语句中使用它:你希望它在哪里收到值?我想你可能想要p.x和p.y——或者不想要。我想知道p是否是point的缩写,但它显然是很多东西。也许你想要更好的变量名。我正在尝试生成一个多项式,所以我需要它你所说的定义是什么意思?我搞不懂。
import random
import sage
import numpy

def flip():
    if random.uniform(0,1) < 0.5:
      return 0
return 1

def a():
  L= [[6,0,0],[0,0,6],[0,6,0],[5,1,0],[1,5,0],[0,5,1],[0,1,5],[4,2,0],[4,0,2],[0,4,2],[0,2,4],[2,0,4],[2,4,0],[4,1,1],[1,1,4],[1,4,1],[3,3,0],[0,3,3],[3,0,3],[3,2,1],[1,2,3],[3,1,2],[2,1,3],[2,3,1],[1,3,2],[0,0,0],[0,0,1],[0,1,0],[1,0,0],[0,0,2],[2,0,0],[0,2,0],[0,0,3],[0,3,0],[3,0,0],[0,0,4],[0,4,0],[4,0,0],[0,0,5],[0,5,0],[5,0,0],[1,0,4],[1,0,3],[1,0,2],[1,0,1],[1,4,0],[1,3,0],[1,2,0],[1,1,0],[2,0,3],[2,0,2],[2,0,1],[2,3,0],[2,2,0],[2,1,0],[3,2,0],[3,1,0],[3,0,1],[3,0,2],[4,1,0],[4,0,1]];
  print(len(L));
  L6 = [m for m in L if sum(m)==6];
  print(len(L6));
  Lless =[m for m in L if m not in L6];
  print(len(Lless));
  S = [];
  for i in range(len(L6)):
    if flip():
      S.append(L6[i])
  if S == []:
    S=[L6[0]]
  for i in range (len(Lless)):
    if flip():
      S.append(Lless[i])
  P = [[ZZ.random_element(), m] for m in S]


  return sum([p[0]*x**p[1][0]*y**p[1][1]*z**p[1][2] for p in P])



print(a());
61
25
36
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-9a3fb37f35b2> in <module>()
     28   return sum([p[0]*x**p[1][0]*(y**p[1][1])*(z**p[1][2]) for p in P])
     29 
---> 30 print(a());""").strip()

<string> in <module>()

<string> in a()


NameError: global name 'y' is not defined