title: How do I iterate through a countdown timer while checking for user input simultaneously?
body: <p>I am trying to code a chess timer in which there are two timers counting down alternatingly. Only one timer runs at a time, and when a certain input is given by the user, which I am using keyboard.is_pressed(&quot;some_key&quot;) for, one timer pauses and the other begins counting down and vice versa. My problem is that to countdown by intervals of 1 second, I am using time.sleep(1) and the program will not receive user input during this time, so unless the user gives input on exactly the 1 second mark, nothing happens. How can I make the countdown process and the checking for user input process run at the same time?</p>
  <p>Here is the code:</p>
  <pre><code>import time
  import keyboard
  
  
  def timers(t1, t2):
      pause = True
      while t1 or t2:
          if pause:
              while pause:
                  mins1 = t1 // 60
                  secs1 = t1 % 60
                  timer1 = '{:02d}:{:02d}'.format(mins1, secs1)
                  print(&quot;Timer1: &quot;, timer1)
                  time.sleep(1)
                  t1 -= 1
                  if keyboard.is_pressed(&quot;w&quot;):
                      pause = False
                      break
                  else:
                      continue
          else:
              while not pause:
                  mins2 = t2 // 60
                  secs2 = t2 % 60
                  timer2 = '{:02d}:{:02d}'.format(mins2, secs2)
                  print(&quot;Timer2: &quot;, timer2)
                  time.sleep(1)
                  t2 -= 1
                  if keyboard.is_pressed(&quot;w&quot;):
                      pause = True
                      break
  
      print(&quot;Beep&quot;)
  
  
  tWhite = int(input(&quot;Enter the time in seconds for White: &quot;))
  tBlack = int(input(&quot;Enter the time in seconds for Black: &quot;))
  
  timers(tWhite, tBlack)
  </code></pre>