title: Python telling me thread assigned outside function isn't assigned
body: <p>I made this program to have a timed input:</p>
  <pre><code>import time
  from threading import Thread
  
  def get_answer():
      answer = input (&quot;ANSWER THE QUESTION!!!&quot;)
  
  def runtime():
      time.sleep(1)
      timer = timer-1
      print (timer)
      if timer == 0:
          thread1.cancel()
          print (&quot;hi&quot;)
  
  timer = 5
  
  # create two new threads
  thread1 = Thread(target=get_answer)
  thread2 = Thread(target=runtime)
  
  # start the threads
  thread1.start()
  thread2.start()
  
  # wait for the threads to complete
  thread1.join()
  thread2.join()
  </code></pre>
  <p>When I run it though, I get this error:</p>
  <pre><code>Exception in thread Thread-2 (runtime):
  Traceback (most recent call last):
    File &quot;C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\threading.py&quot;, line 1009, in _bootstrap_inner
      self.run()
    File &quot;C:\Users\user1\AppData\Local\Programs\Python\Python310\lib\threading.py&quot;, line 946, in run
      self._target(*self._args, **self._kwargs)
    File &quot;c:\Users\user1\Desktop\import time.py&quot;, line 9, in runtime
      timer = timer-1
  UnboundLocalError: local variable 'timer' referenced before assignment
  </code></pre>
  <p>How can I fix this? I set the variable <code>timer</code> before the thread runs, so why isn't it assigned already?</p>