简体   繁体   English

UnboundLocalError:在全局赋值之前引用的局部变量

[英]UnboundLocalError: local variable referenced before assignment whit global

I receive:我收到:

UnboundLocalError: local variable 'ImgY' referenced before assignment

When I do something like this:当我做这样的事情时:

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

The piece "a()":片段“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]] = " "

I don't understand why since ImgY is a global variable and is defined with "ImgY = 30" on the line below (the global) The rest here我不明白为什么因为 ImgY 是一个全局变量并且在下面的行(全局)中用“ImgY = 30”定义这里的 rest

Inside the snippet片段内

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 don't seem to be assigned. ImgY/ImgX 似乎没有被分配。 So if they are global variables, try with:因此,如果它们是全局变量,请尝试:

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]] = " "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 全局变量变为局部--UnboundLocalError:分配前引用了局部变量 - global var becomes local --UnboundLocalError: local variable referenced before assignment UnboundLocalError:分配前已引用局部变量“ truebomb” - UnboundLocalError: local variable 'truebomb' referenced before assignment UnboundLocalError:分配前已引用局部变量“ cur” - UnboundLocalError: local variable 'cur' referenced before assignment UnboundLocalError:分配前已引用局部变量“ Counter” - UnboundLocalError: local variable 'Counter' referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) UnboundLocalError:分配前已引用局部变量“ strdate” - UnboundLocalError: local variable 'strdate' referenced before assignment UnboundLocalError:赋值之前引用了局部变量“ key” - UnboundLocalError: local variable 'key' referenced before assignment unboundLocalError:赋值前引用了局部变量“loopback” - unboundLocalError: local variable 'loopback' referenced before assignment UnboundLocalError:分配前已引用局部变量“ endProgram” - UnboundLocalError: local variable 'endProgram' referenced before assignment UnboundLocalError:赋值前引用了局部变量“Score” - UnboundLocalError: local variable 'Score' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM