title: Name 'run' is assigned to before global declaration
body: <p>Im very new to python having worked on languages like C++ and C# previously and dont understand the error that the compiler is throwing at me.</p>
  <p>Im trying to create a global bool to stop the threaded function when it is changed to false.</p>
  <p>my code so far is:</p>
  <pre><code>import random
  import math
  import threading
  import time
  
  listOfParticles = []
  listOfThreads = []
  run = False
  
  
  def printList():
      print(&quot;New List :&quot;)
      for i in listOfParticles:
          print(str(i[0]) + &quot;,&quot; + str(i[1]))
  
  def moveParticle(partID):
      while (True):
          if(run == False): return
          else:
              dir = random.randrange(0,360)
              dist = random.random()
              distx = math.cos(math.radians(dir))*dist
              disty = math.sin(math.radians(dir))*dist
              listOfParticles[i][0] += distx
              listOfParticles[i][1] += disty
  
  def startMove():
      global run
      run = True
      for i in range(len(listOfParticles)):
          t = threading.Thread(target=moveParticle, args=(i,))
          listOfThreads.append(t)
      
      for i in listOfThreads:
          i.start()
  
  if __name__ == &quot;__main__&quot;:    
      global run
      for i in range(10):
          listOfParticles.append([random.random(), random.random()])
      startMove()
      while(run):
          printList()
          time.sleep(0.001)
      run = False
  </code></pre>
  <p>And this code is giving me the error</p>
  <pre><code>  File &quot;d:\Programming\repos\Parallel\repos\Python\Lab 4\threadedParticles.py&quot;, line 38
      global run
      ^
  SyntaxError: name 'run' is assigned to before global declaration
  </code></pre>
  <p>What confuses me is that the error is happening on the global definition.</p>