UnboundLocalError:分配全局之前引用的局部变量

时间:2020-10-24 16:24:45

标签: python

我收到:

UnboundLocalError: local variable 'ImgY' referenced before assignment

当我做这样的事情时:

q = 0
def a():
  print(q)
while True:
  com = input("")
  if com == "":
    a()
  else:
    q = com

片断“ a()”:

def df():
  global deas
  if vs != 1:
    if deas == 0:
      deas = 1
      for a in range(ImgY):
        for s in range(ImgX):
          corrente[num[a] + num[s]] = " "
  else:
    if imp == 1:
      ImgX = q[0:1]
      ImgY = q[2:3]
      for a in range(int(ImgY)):
          for s in range(int(ImgX)):
            corrente[num[a] + num[s]] = q[s+5]
    else:
      if deas == 0:
        deas = 1
        for a in range(ImgY):
          for s in range(ImgX):
            corrente[num[a] + num[s]] = " "

我不明白为什么,因为ImgY是全局变量,并且在下面的行(全局)中用“ ImgY = 30”定义 其余here

1 个答案:

答案 0 :(得分:0)

代码段内

def df():
  global deas
  if vs != 1:
    if deas == 0:
      deas = 1
      for a in range(ImgY):
        for s in range(ImgX):
          corrente[num[a] + num[s]] = " "

ImgY / ImgX似乎没有分配。因此,如果它们是全局变量,请尝试:

def df():
  global deas,ImgY,ImgX
  if vs != 1:
    if deas == 0:
      deas = 1
      for a in range(ImgY):
        for s in range(ImgX):
          corrente[num[a] + num[s]] = " "