1

I've set a global variable delete at the start of the project, right below the libraries imported and before any class, yet when I have this bit of code:

def motion_notify_callback(event):
        if (ispymol == True):

            if event.inaxes is ax:      
            x = event.xdata
            y = event.ydata
            x = round(x,0)
            y = round(y,0)
            x = int(x)
            y = int(y)
            coord = (x,y)


            for i in range(0,len(number_list)):

                if (coord == number_list[i]):
                if (delete == True):
                    pymol.cmd.do("delete CurrentCont")
                    delete = False
                pymol.cmd.do("distance CurrentCont, chain"+lc+" and resi "+resi1[i]+" and name CA, chain"+lc+" and resi "+resi2[i]+" and name CA")

                    delete = True

            for i in range(0,len(rres)):
            if (coord == mappingpredcont[i]):
               if (delete == True):
                       pymol.cmd.do("delete CurrentCont")
                       delete =False
               pymol.cmd.do("distance CurrentCont, chain"+lc+" and resi "+predresi1[i]+" and name CA, chain"+lc+" and resi "+predresi2[i]+" and name CA")

                   delete = True

It has the error local variable 'delete' referenced before assignment for global variable Where am I going wrong?

miik
  • 661
  • 1
  • 7
  • 19
  • 1
    You have to define `global` in every function where you want to use it (to remind you that in >99% of all usage cases it's bad coding style). – Matthias Apr 26 '13 at 09:24

2 Answers2

3

You need to tell Python that you intend to assign to a global variable:

def motion_notify_callback(event):
    global delete
    ...
thebjorn
  • 24,226
  • 9
  • 82
  • 127
3

You need to define delete as a global at the beginning of your function:

def motion_notify_callback(event):
   global delete
   .....
Gareth Webber
  • 4,158
  • 1
  • 23
  • 27