0

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()":

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

  • without understand your code, I ran it with several inputs (with any input and without to make sure I come across 2 options and didnt faced any issue) can you provide example of input with error, please not a an image – Yossi Levi Oct 24 '20 at 16:28
  • Does this answer your question? [Don't understand why UnboundLocalError occurs (closure)](https://stackoverflow.com/questions/9264763/dont-understand-why-unboundlocalerror-occurs-closure) – Tomerikoo Oct 24 '20 at 16:31
  • The line `ImgY = q[2:3]` makes this variable local to the `df` function. Then you try to access it (`for a in range(ImgY):`) before it was actually defined. If you want to **assign** to a global variable in a function, you must use the `global` keyword – Tomerikoo Oct 24 '20 at 16:32
  • to get the error: 0 line break si line break 0l ine break – scaricraft Oct 24 '20 at 16:33
  • I corrected "ImgY = q[2:3]" with "ImgY = int(q[2:3])" but it doesn't change – scaricraft Oct 24 '20 at 16:39
  • It is normal that you do not understand the code: it is written in Italian and overall it sucks enough. However it aspires to become a "" "" graphics engine "" "" – scaricraft Oct 24 '20 at 16:48

1 Answers1

0

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. 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]] = " "
Wasif
  • 14,755
  • 3
  • 14
  • 34